home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / gcc270_src.lha / gcc-2.7.0-amiga / combine.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  378KB  |  11,041 lines

  1. /* Optimize by combining instructions for GNU compiler.
  2.    Copyright (C) 1987, 88, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21.  
  22. /* This module is essentially the "combiner" phase of the U. of Arizona
  23.    Portable Optimizer, but redone to work on our list-structured
  24.    representation for RTL instead of their string representation.
  25.  
  26.    The LOG_LINKS of each insn identify the most recent assignment
  27.    to each REG used in the insn.  It is a list of previous insns,
  28.    each of which contains a SET for a REG that is used in this insn
  29.    and not used or set in between.  LOG_LINKs never cross basic blocks.
  30.    They were set up by the preceding pass (lifetime analysis).
  31.  
  32.    We try to combine each pair of insns joined by a logical link.
  33.    We also try to combine triples of insns A, B and C when
  34.    C has a link back to B and B has a link back to A.
  35.  
  36.    LOG_LINKS does not have links for use of the CC0.  They don't
  37.    need to, because the insn that sets the CC0 is always immediately
  38.    before the insn that tests it.  So we always regard a branch
  39.    insn as having a logical link to the preceding insn.  The same is true
  40.    for an insn explicitly using CC0.
  41.  
  42.    We check (with use_crosses_set_p) to avoid combining in such a way
  43.    as to move a computation to a place where its value would be different.
  44.  
  45.    Combination is done by mathematically substituting the previous
  46.    insn(s) values for the regs they set into the expressions in
  47.    the later insns that refer to these regs.  If the result is a valid insn
  48.    for our target machine, according to the machine description,
  49.    we install it, delete the earlier insns, and update the data flow
  50.    information (LOG_LINKS and REG_NOTES) for what we did.
  51.  
  52.    There are a few exceptions where the dataflow information created by
  53.    flow.c aren't completely updated:
  54.  
  55.    - reg_live_length is not updated
  56.    - reg_n_refs is not adjusted in the rare case when a register is
  57.      no longer required in a computation
  58.    - there are extremely rare cases (see distribute_regnotes) when a
  59.      REG_DEAD note is lost
  60.    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
  61.      removed because there is no way to know which register it was 
  62.      linking
  63.  
  64.    To simplify substitution, we combine only when the earlier insn(s)
  65.    consist of only a single assignment.  To simplify updating afterward,
  66.    we never combine when a subroutine call appears in the middle.
  67.  
  68.    Since we do not represent assignments to CC0 explicitly except when that
  69.    is all an insn does, there is no LOG_LINKS entry in an insn that uses
  70.    the condition code for the insn that set the condition code.
  71.    Fortunately, these two insns must be consecutive.
  72.    Therefore, every JUMP_INSN is taken to have an implicit logical link
  73.    to the preceding insn.  This is not quite right, since non-jumps can
  74.    also use the condition code; but in practice such insns would not
  75.    combine anyway.  */
  76.  
  77. #include "config.h"
  78. #ifdef __STDC__
  79. #include <stdarg.h>
  80. #else
  81. #include <varargs.h>
  82. #endif
  83.  
  84. /* Must precede rtl.h for FFS.  */
  85. #include <stdio.h>
  86.  
  87. #include "rtl.h"
  88. #include "flags.h"
  89. #include "regs.h"
  90. #include "hard-reg-set.h"
  91. #include "expr.h"
  92. #include "basic-block.h"
  93. #include "insn-config.h"
  94. #include "insn-flags.h"
  95. #include "insn-codes.h"
  96. #include "insn-attr.h"
  97. #include "recog.h"
  98. #include "real.h"
  99.  
  100. /* It is not safe to use ordinary gen_lowpart in combine.
  101.    Use gen_lowpart_for_combine instead.  See comments there.  */
  102. #define gen_lowpart dont_use_gen_lowpart_you_dummy
  103.  
  104. /* Number of attempts to combine instructions in this function.  */
  105.  
  106. static int combine_attempts;
  107.  
  108. /* Number of attempts that got as far as substitution in this function.  */
  109.  
  110. static int combine_merges;
  111.  
  112. /* Number of instructions combined with added SETs in this function.  */
  113.  
  114. static int combine_extras;
  115.  
  116. /* Number of instructions combined in this function.  */
  117.  
  118. static int combine_successes;
  119.  
  120. /* Totals over entire compilation.  */
  121.  
  122. static int total_attempts, total_merges, total_extras, total_successes;
  123.  
  124. /* Define a default value for REVERSIBLE_CC_MODE.
  125.    We can never assume that a condition code mode is safe to reverse unless
  126.    the md tells us so.  */
  127. #ifndef REVERSIBLE_CC_MODE
  128. #define REVERSIBLE_CC_MODE(MODE) 0
  129. #endif
  130.  
  131. /* Vector mapping INSN_UIDs to cuids.
  132.    The cuids are like uids but increase monotonically always.
  133.    Combine always uses cuids so that it can compare them.
  134.    But actually renumbering the uids, which we used to do,
  135.    proves to be a bad idea because it makes it hard to compare
  136.    the dumps produced by earlier passes with those from later passes.  */
  137.  
  138. static int *uid_cuid;
  139. static int max_uid_cuid;
  140.  
  141. /* Get the cuid of an insn.  */
  142.  
  143. #define INSN_CUID(INSN) (INSN_UID (INSN) > max_uid_cuid        \
  144.              ? (abort(), 0)                \
  145.              : uid_cuid[INSN_UID (INSN)])
  146.  
  147. /* Maximum register number, which is the size of the tables below.  */
  148.  
  149. static int combine_max_regno;
  150.  
  151. /* Record last point of death of (hard or pseudo) register n.  */
  152.  
  153. static rtx *reg_last_death;
  154.  
  155. /* Record last point of modification of (hard or pseudo) register n.  */
  156.  
  157. static rtx *reg_last_set;
  158.  
  159. /* Record the cuid of the last insn that invalidated memory
  160.    (anything that writes memory, and subroutine calls, but not pushes).  */
  161.  
  162. static int mem_last_set;
  163.  
  164. /* Record the cuid of the last CALL_INSN
  165.    so we can tell whether a potential combination crosses any calls.  */
  166.  
  167. static int last_call_cuid;
  168.  
  169. /* When `subst' is called, this is the insn that is being modified
  170.    (by combining in a previous insn).  The PATTERN of this insn
  171.    is still the old pattern partially modified and it should not be
  172.    looked at, but this may be used to examine the successors of the insn
  173.    to judge whether a simplification is valid.  */
  174.  
  175. static rtx subst_insn;
  176.  
  177. /* This is an insn that belongs before subst_insn, but is not currently
  178.    on the insn chain.  */
  179.  
  180. static rtx subst_prev_insn;
  181.  
  182. /* This is the lowest CUID that `subst' is currently dealing with.
  183.    get_last_value will not return a value if the register was set at or
  184.    after this CUID.  If not for this mechanism, we could get confused if
  185.    I2 or I1 in try_combine were an insn that used the old value of a register
  186.    to obtain a new value.  In that case, we might erroneously get the
  187.    new value of the register when we wanted the old one.  */
  188.  
  189. static int subst_low_cuid;
  190.  
  191. /* This contains any hard registers that are used in newpat; reg_dead_at_p
  192.    must consider all these registers to be always live.  */
  193.  
  194. static HARD_REG_SET newpat_used_regs;
  195.  
  196. /* This is an insn to which a LOG_LINKS entry has been added.  If this
  197.    insn is the earlier than I2 or I3, combine should rescan starting at
  198.    that location.  */
  199.  
  200. static rtx added_links_insn;
  201.  
  202. /* This is the value of undobuf.num_undo when we started processing this 
  203.    substitution.  This will prevent gen_rtx_combine from re-used a piece
  204.    from the previous expression.  Doing so can produce circular rtl
  205.    structures.  */
  206.  
  207. static int previous_num_undos;
  208.  
  209. /* Basic block number of the block in which we are performing combines.  */
  210. static int this_basic_block;
  211.  
  212. /* The next group of arrays allows the recording of the last value assigned
  213.    to (hard or pseudo) register n.  We use this information to see if a
  214.    operation being processed is redundant given a prior operation performed
  215.    on the register.  For example, an `and' with a constant is redundant if
  216.    all the zero bits are already known to be turned off.
  217.  
  218.    We use an approach similar to that used by cse, but change it in the
  219.    following ways:
  220.  
  221.    (1) We do not want to reinitialize at each label.
  222.    (2) It is useful, but not critical, to know the actual value assigned
  223.        to a register.  Often just its form is helpful.
  224.  
  225.    Therefore, we maintain the following arrays:
  226.  
  227.    reg_last_set_value        the last value assigned
  228.    reg_last_set_label        records the value of label_tick when the
  229.                 register was assigned
  230.    reg_last_set_table_tick    records the value of label_tick when a
  231.                 value using the register is assigned
  232.    reg_last_set_invalid        set to non-zero when it is not valid
  233.                 to use the value of this register in some
  234.                 register's value
  235.  
  236.    To understand the usage of these tables, it is important to understand
  237.    the distinction between the value in reg_last_set_value being valid
  238.    and the register being validly contained in some other expression in the
  239.    table.
  240.  
  241.    Entry I in reg_last_set_value is valid if it is non-zero, and either
  242.    reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
  243.  
  244.    Register I may validly appear in any expression returned for the value
  245.    of another register if reg_n_sets[i] is 1.  It may also appear in the
  246.    value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
  247.    reg_last_set_invalid[j] is zero.
  248.  
  249.    If an expression is found in the table containing a register which may
  250.    not validly appear in an expression, the register is replaced by
  251.    something that won't match, (clobber (const_int 0)).
  252.  
  253.    reg_last_set_invalid[i] is set non-zero when register I is being assigned
  254.    to and reg_last_set_table_tick[i] == label_tick.  */
  255.  
  256. /* Record last value assigned to (hard or pseudo) register n. */
  257.  
  258. static rtx *reg_last_set_value;
  259.  
  260. /* Record the value of label_tick when the value for register n is placed in
  261.    reg_last_set_value[n].  */
  262.  
  263. static int *reg_last_set_label;
  264.  
  265. /* Record the value of label_tick when an expression involving register n
  266.    is placed in reg_last_set_value. */
  267.  
  268. static int *reg_last_set_table_tick;
  269.  
  270. /* Set non-zero if references to register n in expressions should not be
  271.    used.  */
  272.  
  273. static char *reg_last_set_invalid;
  274.  
  275. /* Incremented for each label. */
  276.  
  277. static int label_tick;
  278.  
  279. /* Some registers that are set more than once and used in more than one
  280.    basic block are nevertheless always set in similar ways.  For example,
  281.    a QImode register may be loaded from memory in two places on a machine
  282.    where byte loads zero extend.
  283.  
  284.    We record in the following array what we know about the nonzero
  285.    bits of a register, specifically which bits are known to be zero.
  286.  
  287.    If an entry is zero, it means that we don't know anything special.  */
  288.  
  289. static unsigned HOST_WIDE_INT *reg_nonzero_bits;
  290.  
  291. /* Mode used to compute significance in reg_nonzero_bits.  It is the largest
  292.    integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
  293.  
  294. static enum machine_mode nonzero_bits_mode;
  295.  
  296. /* Nonzero if we know that a register has some leading bits that are always
  297.    equal to the sign bit.  */
  298.  
  299. static char *reg_sign_bit_copies;
  300.  
  301. /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
  302.    It is zero while computing them and after combine has completed.  This
  303.    former test prevents propagating values based on previously set values,
  304.    which can be incorrect if a variable is modified in a loop.  */
  305.  
  306. static int nonzero_sign_valid;
  307.  
  308. /* These arrays are maintained in parallel with reg_last_set_value
  309.    and are used to store the mode in which the register was last set,
  310.    the bits that were known to be zero when it was last set, and the
  311.    number of sign bits copies it was known to have when it was last set.  */
  312.  
  313. static enum machine_mode *reg_last_set_mode;
  314. static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
  315. static char *reg_last_set_sign_bit_copies;
  316.  
  317. /* Record one modification to rtl structure
  318.    to be undone by storing old_contents into *where.
  319.    is_int is 1 if the contents are an int.  */
  320.  
  321. struct undo
  322. {
  323.   int is_int;
  324.   union {rtx r; int i;} old_contents;
  325.   union {rtx *r; int *i;} where;
  326. };
  327.  
  328. /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
  329.    num_undo says how many are currently recorded.
  330.  
  331.    storage is nonzero if we must undo the allocation of new storage.
  332.    The value of storage is what to pass to obfree.
  333.  
  334.    other_insn is nonzero if we have modified some other insn in the process
  335.    of working on subst_insn.  It must be verified too.  */
  336.  
  337. #define MAX_UNDO 50
  338.  
  339. struct undobuf
  340. {
  341.   int num_undo;
  342.   char *storage;
  343.   struct undo undo[MAX_UNDO];
  344.   rtx other_insn;
  345. };
  346.  
  347. static struct undobuf undobuf;
  348.  
  349. /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
  350.    insn.  The substitution can be undone by undo_all.  If INTO is already
  351.    set to NEWVAL, do not record this change.  Because computing NEWVAL might
  352.    also call SUBST, we have to compute it before we put anything into
  353.    the undo table.  */
  354.  
  355. #define SUBST(INTO, NEWVAL)  \
  356.  do { rtx _new = (NEWVAL);                        \
  357.       if (undobuf.num_undo < MAX_UNDO)                    \
  358.     {                                \
  359.       undobuf.undo[undobuf.num_undo].is_int = 0;            \
  360.       undobuf.undo[undobuf.num_undo].where.r = &INTO;        \
  361.       undobuf.undo[undobuf.num_undo].old_contents.r = INTO;    \
  362.       INTO = _new;                            \
  363.       if (undobuf.undo[undobuf.num_undo].old_contents.r != INTO)    \
  364.         undobuf.num_undo++;                     \
  365.     }                                \
  366.     } while (0)
  367.  
  368. /* Similar to SUBST, but NEWVAL is an int.  INTO will normally be an XINT
  369.    expression.
  370.    Note that substitution for the value of a CONST_INT is not safe.  */
  371.  
  372. #define SUBST_INT(INTO, NEWVAL)  \
  373.  do { if (undobuf.num_undo < MAX_UNDO)                    \
  374. {                                    \
  375.       undobuf.undo[undobuf.num_undo].is_int = 1;            \
  376.       undobuf.undo[undobuf.num_undo].where.i = (int *) &INTO;    \
  377.       undobuf.undo[undobuf.num_undo].old_contents.i = INTO;        \
  378.       INTO = NEWVAL;                        \
  379.       if (undobuf.undo[undobuf.num_undo].old_contents.i != INTO)    \
  380.         undobuf.num_undo++;                        \
  381.     }                                \
  382.      } while (0)
  383.  
  384. /* Number of times the pseudo being substituted for
  385.    was found and replaced.  */
  386.  
  387. static int n_occurrences;
  388.  
  389. static void init_reg_last_arrays    PROTO(());
  390. static void setup_incoming_promotions   PROTO(());
  391. static void set_nonzero_bits_and_sign_copies  PROTO((rtx, rtx));
  392. static int can_combine_p    PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
  393. static int combinable_i3pat    PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
  394. static rtx try_combine        PROTO((rtx, rtx, rtx));
  395. static void undo_all        PROTO((void));
  396. static rtx *find_split_point    PROTO((rtx *, rtx));
  397. static rtx subst        PROTO((rtx, rtx, rtx, int, int));
  398. static rtx simplify_rtx        PROTO((rtx, enum machine_mode, int, int));
  399. static rtx simplify_if_then_else  PROTO((rtx));
  400. static rtx simplify_set        PROTO((rtx));
  401. static rtx simplify_logical    PROTO((rtx, int));
  402. static rtx expand_compound_operation  PROTO((rtx));
  403. static rtx expand_field_assignment  PROTO((rtx));
  404. static rtx make_extraction    PROTO((enum machine_mode, rtx, int, rtx, int,
  405.                        int, int, int));
  406. static rtx extract_left_shift    PROTO((rtx, int));
  407. static rtx make_compound_operation  PROTO((rtx, enum rtx_code));
  408. static int get_pos_from_mask    PROTO((unsigned HOST_WIDE_INT, int *));
  409. static rtx force_to_mode    PROTO((rtx, enum machine_mode,
  410.                        unsigned HOST_WIDE_INT, rtx, int));
  411. static rtx if_then_else_cond    PROTO((rtx, rtx *, rtx *));
  412. static rtx known_cond        PROTO((rtx, enum rtx_code, rtx, rtx));
  413. static rtx make_field_assignment  PROTO((rtx));
  414. static rtx apply_distributive_law  PROTO((rtx));
  415. static rtx simplify_and_const_int  PROTO((rtx, enum machine_mode, rtx,
  416.                       unsigned HOST_WIDE_INT));
  417. static unsigned HOST_WIDE_INT nonzero_bits  PROTO((rtx, enum machine_mode));
  418. static int num_sign_bit_copies  PROTO((rtx, enum machine_mode));
  419. static int merge_outer_ops    PROTO((enum rtx_code *, HOST_WIDE_INT *,
  420.                        enum rtx_code, HOST_WIDE_INT,
  421.                        enum machine_mode, int *));
  422. static rtx simplify_shift_const    PROTO((rtx, enum rtx_code, enum machine_mode,
  423.                        rtx, int));
  424. static int recog_for_combine    PROTO((rtx *, rtx, rtx *, int *));
  425. static rtx gen_lowpart_for_combine  PROTO((enum machine_mode, rtx));
  426. static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
  427.                   ...));
  428. static rtx gen_binary        PROTO((enum rtx_code, enum machine_mode,
  429.                        rtx, rtx));
  430. static rtx gen_unary        PROTO((enum rtx_code, enum machine_mode,
  431.                        enum machine_mode, rtx));
  432. static enum rtx_code simplify_comparison  PROTO((enum rtx_code, rtx *, rtx *));
  433. static int reversible_comparison_p  PROTO((rtx));
  434. static void update_table_tick    PROTO((rtx));
  435. static void record_value_for_reg  PROTO((rtx, rtx, rtx));
  436. static void record_dead_and_set_regs_1  PROTO((rtx, rtx));
  437. static void record_dead_and_set_regs  PROTO((rtx));
  438. static int get_last_value_validate  PROTO((rtx *, int, int));
  439. static rtx get_last_value    PROTO((rtx));
  440. static int use_crosses_set_p    PROTO((rtx, int));
  441. static void reg_dead_at_p_1    PROTO((rtx, rtx));
  442. static int reg_dead_at_p    PROTO((rtx, rtx));
  443. static void move_deaths        PROTO((rtx, int, rtx, rtx *));
  444. static int reg_bitfield_target_p  PROTO((rtx, rtx));
  445. static void distribute_notes    PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
  446. static void distribute_links    PROTO((rtx));
  447. static void mark_used_regs_combine PROTO((rtx));
  448.  
  449. /* Main entry point for combiner.  F is the first insn of the function.
  450.    NREGS is the first unused pseudo-reg number.  */
  451.  
  452. void
  453. combine_instructions (f, nregs)
  454.      rtx f;
  455.      int nregs;
  456. {
  457.   register rtx insn, next, prev;
  458.   register int i;
  459.   register rtx links, nextlinks;
  460.  
  461.   combine_attempts = 0;
  462.   combine_merges = 0;
  463.   combine_extras = 0;
  464.   combine_successes = 0;
  465.   undobuf.num_undo = previous_num_undos = 0;
  466.  
  467.   combine_max_regno = nregs;
  468.  
  469.   reg_nonzero_bits
  470.     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
  471.   reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
  472.  
  473.   bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
  474.   bzero (reg_sign_bit_copies, nregs * sizeof (char));
  475.  
  476.   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
  477.   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
  478.   reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
  479.   reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
  480.   reg_last_set_label = (int *) alloca (nregs * sizeof (int));
  481.   reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
  482.   reg_last_set_mode
  483.     = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
  484.   reg_last_set_nonzero_bits
  485.     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
  486.   reg_last_set_sign_bit_copies
  487.     = (char *) alloca (nregs * sizeof (char));
  488.  
  489.   init_reg_last_arrays ();
  490.  
  491.   init_recog_no_volatile ();
  492.  
  493.   /* Compute maximum uid value so uid_cuid can be allocated.  */
  494.  
  495.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  496.     if (INSN_UID (insn) > i)
  497.       i = INSN_UID (insn);
  498.  
  499.   uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
  500.   max_uid_cuid = i;
  501.  
  502.   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
  503.  
  504.   /* Don't use reg_nonzero_bits when computing it.  This can cause problems
  505.      when, for example, we have j <<= 1 in a loop.  */
  506.  
  507.   nonzero_sign_valid = 0;
  508.  
  509.   /* Compute the mapping from uids to cuids.
  510.      Cuids are numbers assigned to insns, like uids,
  511.      except that cuids increase monotonically through the code. 
  512.  
  513.      Scan all SETs and see if we can deduce anything about what
  514.      bits are known to be zero for some registers and how many copies
  515.      of the sign bit are known to exist for those registers.
  516.  
  517.      Also set any known values so that we can use it while searching
  518.      for what bits are known to be set.  */
  519.  
  520.   label_tick = 1;
  521.  
  522.   /* We need to initialize it here, because record_dead_and_set_regs may call
  523.      get_last_value.  */
  524.   subst_prev_insn = NULL_RTX;
  525.  
  526.   setup_incoming_promotions ();
  527.  
  528.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  529.     {
  530.       uid_cuid[INSN_UID (insn)] = ++i;
  531.       subst_low_cuid = i;
  532.       subst_insn = insn;
  533.  
  534.       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  535.     {
  536.       note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
  537.       record_dead_and_set_regs (insn);
  538.     }
  539.  
  540.       if (GET_CODE (insn) == CODE_LABEL)
  541.     label_tick++;
  542.     }
  543.  
  544.   nonzero_sign_valid = 1;
  545.  
  546.   /* Now scan all the insns in forward order.  */
  547.  
  548.   this_basic_block = -1;
  549.   label_tick = 1;
  550.   last_call_cuid = 0;
  551.   mem_last_set = 0;
  552.   init_reg_last_arrays ();
  553.   setup_incoming_promotions ();
  554.  
  555.   for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
  556.     {
  557.       next = 0;
  558.  
  559.       /* If INSN starts a new basic block, update our basic block number.  */
  560.       if (this_basic_block + 1 < n_basic_blocks
  561.       && basic_block_head[this_basic_block + 1] == insn)
  562.     this_basic_block++;
  563.  
  564.       if (GET_CODE (insn) == CODE_LABEL)
  565.     label_tick++;
  566.  
  567.       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  568.     {
  569.       /* Try this insn with each insn it links back to.  */
  570.  
  571.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  572.         if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
  573.           goto retry;
  574.  
  575.       /* Try each sequence of three linked insns ending with this one.  */
  576.  
  577.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  578.         for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
  579.          nextlinks = XEXP (nextlinks, 1))
  580.           if ((next = try_combine (insn, XEXP (links, 0),
  581.                        XEXP (nextlinks, 0))) != 0)
  582.         goto retry;
  583.  
  584. #ifdef HAVE_cc0
  585.       /* Try to combine a jump insn that uses CC0
  586.          with a preceding insn that sets CC0, and maybe with its
  587.          logical predecessor as well.
  588.          This is how we make decrement-and-branch insns.
  589.          We need this special code because data flow connections
  590.          via CC0 do not get entered in LOG_LINKS.  */
  591.  
  592.       if (GET_CODE (insn) == JUMP_INSN
  593.           && (prev = prev_nonnote_insn (insn)) != 0
  594.           && GET_CODE (prev) == INSN
  595.           && sets_cc0_p (PATTERN (prev)))
  596.         {
  597.           if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
  598.         goto retry;
  599.  
  600.           for (nextlinks = LOG_LINKS (prev); nextlinks;
  601.            nextlinks = XEXP (nextlinks, 1))
  602.         if ((next = try_combine (insn, prev,
  603.                      XEXP (nextlinks, 0))) != 0)
  604.           goto retry;
  605.         }
  606.  
  607.       /* Do the same for an insn that explicitly references CC0.  */
  608.       if (GET_CODE (insn) == INSN
  609.           && (prev = prev_nonnote_insn (insn)) != 0
  610.           && GET_CODE (prev) == INSN
  611.           && sets_cc0_p (PATTERN (prev))
  612.           && GET_CODE (PATTERN (insn)) == SET
  613.           && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
  614.         {
  615.           if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
  616.         goto retry;
  617.  
  618.           for (nextlinks = LOG_LINKS (prev); nextlinks;
  619.            nextlinks = XEXP (nextlinks, 1))
  620.         if ((next = try_combine (insn, prev,
  621.                      XEXP (nextlinks, 0))) != 0)
  622.           goto retry;
  623.         }
  624.  
  625.       /* Finally, see if any of the insns that this insn links to
  626.          explicitly references CC0.  If so, try this insn, that insn,
  627.          and its predecessor if it sets CC0.  */
  628.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  629.         if (GET_CODE (XEXP (links, 0)) == INSN
  630.         && GET_CODE (PATTERN (XEXP (links, 0))) == SET
  631.         && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
  632.         && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
  633.         && GET_CODE (prev) == INSN
  634.         && sets_cc0_p (PATTERN (prev))
  635.         && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
  636.           goto retry;
  637. #endif
  638.  
  639.       /* Try combining an insn with two different insns whose results it
  640.          uses.  */
  641.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  642.         for (nextlinks = XEXP (links, 1); nextlinks;
  643.          nextlinks = XEXP (nextlinks, 1))
  644.           if ((next = try_combine (insn, XEXP (links, 0),
  645.                        XEXP (nextlinks, 0))) != 0)
  646.         goto retry;
  647.  
  648.       if (GET_CODE (insn) != NOTE)
  649.         record_dead_and_set_regs (insn);
  650.  
  651.     retry:
  652.       ;
  653.     }
  654.     }
  655.  
  656.   total_attempts += combine_attempts;
  657.   total_merges += combine_merges;
  658.   total_extras += combine_extras;
  659.   total_successes += combine_successes;
  660.  
  661.   nonzero_sign_valid = 0;
  662. }
  663.  
  664. /* Wipe the reg_last_xxx arrays in preparation for another pass.  */
  665.  
  666. static void
  667. init_reg_last_arrays ()
  668. {
  669.   int nregs = combine_max_regno;
  670.  
  671.   bzero ((char *) reg_last_death, nregs * sizeof (rtx));
  672.   bzero ((char *) reg_last_set, nregs * sizeof (rtx));
  673.   bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
  674.   bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
  675.   bzero ((char *) reg_last_set_label, nregs * sizeof (int));
  676.   bzero (reg_last_set_invalid, nregs * sizeof (char));
  677.   bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
  678.   bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
  679.   bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
  680. }
  681.  
  682. /* Set up any promoted values for incoming argument registers.  */
  683.  
  684. static void
  685. setup_incoming_promotions ()
  686. {
  687. #ifdef PROMOTE_FUNCTION_ARGS
  688.   int regno;
  689.   rtx reg;
  690.   enum machine_mode mode;
  691.   int unsignedp;
  692.   rtx first = get_insns ();
  693.  
  694.   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
  695.     if (FUNCTION_ARG_REGNO_P (regno)
  696.     && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
  697.       record_value_for_reg (reg, first,
  698.                 gen_rtx (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
  699.                      GET_MODE (reg),
  700.                      gen_rtx (CLOBBER, mode, const0_rtx)));
  701. #endif
  702. }
  703.  
  704. /* Called via note_stores.  If X is a pseudo that is used in more than
  705.    one basic block, is narrower that HOST_BITS_PER_WIDE_INT, and is being
  706.    set, record what bits are known zero.  If we are clobbering X,
  707.    ignore this "set" because the clobbered value won't be used. 
  708.  
  709.    If we are setting only a portion of X and we can't figure out what
  710.    portion, assume all bits will be used since we don't know what will
  711.    be happening.
  712.  
  713.    Similarly, set how many bits of X are known to be copies of the sign bit
  714.    at all locations in the function.  This is the smallest number implied 
  715.    by any set of X.  */
  716.  
  717. static void
  718. set_nonzero_bits_and_sign_copies (x, set)
  719.      rtx x;
  720.      rtx set;
  721. {
  722.   int num;
  723.  
  724.   if (GET_CODE (x) == REG
  725.       && REGNO (x) >= FIRST_PSEUDO_REGISTER
  726.       && reg_n_sets[REGNO (x)] > 1
  727.       && reg_basic_block[REGNO (x)] < 0
  728.       /* If this register is undefined at the start of the file, we can't
  729.      say what its contents were.  */
  730.       && ! (basic_block_live_at_start[0][REGNO (x) / REGSET_ELT_BITS]
  731.         & ((REGSET_ELT_TYPE) 1 << (REGNO (x) % REGSET_ELT_BITS)))
  732.       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
  733.     {
  734.       if (GET_CODE (set) == CLOBBER)
  735.     {
  736.       reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
  737.       reg_sign_bit_copies[REGNO (x)] = 0;
  738.       return;
  739.     }
  740.  
  741.       /* If this is a complex assignment, see if we can convert it into a
  742.      simple assignment.  */
  743.       set = expand_field_assignment (set);
  744.  
  745.       /* If this is a simple assignment, or we have a paradoxical SUBREG,
  746.      set what we know about X.  */
  747.  
  748.       if (SET_DEST (set) == x
  749.       || (GET_CODE (SET_DEST (set)) == SUBREG
  750.           && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
  751.           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
  752.           && SUBREG_REG (SET_DEST (set)) == x))
  753.     {
  754.       rtx src = SET_SRC (set);
  755.  
  756. #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
  757.       /* If X is narrower than a word and SRC is a non-negative
  758.          constant that would appear negative in the mode of X,
  759.          sign-extend it for use in reg_nonzero_bits because some
  760.          machines (maybe most) will actually do the sign-extension
  761.          and this is the conservative approach. 
  762.  
  763.          ??? For 2.5, try to tighten up the MD files in this regard
  764.          instead of this kludge.  */
  765.  
  766.       if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
  767.           && GET_CODE (src) == CONST_INT
  768.           && INTVAL (src) > 0
  769.           && 0 != (INTVAL (src)
  770.                & ((HOST_WIDE_INT) 1
  771.               << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
  772.         src = GEN_INT (INTVAL (src)
  773.                | ((HOST_WIDE_INT) (-1)
  774.                   << GET_MODE_BITSIZE (GET_MODE (x))));
  775. #endif
  776.  
  777.       reg_nonzero_bits[REGNO (x)]
  778.         |= nonzero_bits (src, nonzero_bits_mode);
  779.       num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
  780.       if (reg_sign_bit_copies[REGNO (x)] == 0
  781.           || reg_sign_bit_copies[REGNO (x)] > num)
  782.         reg_sign_bit_copies[REGNO (x)] = num;
  783.     }
  784.       else
  785.     {
  786.       reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
  787.       reg_sign_bit_copies[REGNO (x)] = 0;
  788.     }
  789.     }
  790. }
  791.  
  792. /* See if INSN can be combined into I3.  PRED and SUCC are optionally
  793.    insns that were previously combined into I3 or that will be combined
  794.    into the merger of INSN and I3.
  795.  
  796.    Return 0 if the combination is not allowed for any reason.
  797.  
  798.    If the combination is allowed, *PDEST will be set to the single 
  799.    destination of INSN and *PSRC to the single source, and this function
  800.    will return 1.  */
  801.  
  802. static int
  803. can_combine_p (insn, i3, pred, succ, pdest, psrc)
  804.      rtx insn;
  805.      rtx i3;
  806.      rtx pred, succ;
  807.      rtx *pdest, *psrc;
  808. {
  809.   int i;
  810.   rtx set = 0, src, dest;
  811.   rtx p, link;
  812.   int all_adjacent = (succ ? (next_active_insn (insn) == succ
  813.                   && next_active_insn (succ) == i3)
  814.               : next_active_insn (insn) == i3);
  815.  
  816.   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
  817.      or a PARALLEL consisting of such a SET and CLOBBERs. 
  818.  
  819.      If INSN has CLOBBER parallel parts, ignore them for our processing.
  820.      By definition, these happen during the execution of the insn.  When it
  821.      is merged with another insn, all bets are off.  If they are, in fact,
  822.      needed and aren't also supplied in I3, they may be added by
  823.      recog_for_combine.  Otherwise, it won't match. 
  824.  
  825.      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
  826.      note.
  827.  
  828.      Get the source and destination of INSN.  If more than one, can't 
  829.      combine.  */
  830.      
  831.   if (GET_CODE (PATTERN (insn)) == SET)
  832.     set = PATTERN (insn);
  833.   else if (GET_CODE (PATTERN (insn)) == PARALLEL
  834.        && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
  835.     {
  836.       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
  837.     {
  838.       rtx elt = XVECEXP (PATTERN (insn), 0, i);
  839.  
  840.       switch (GET_CODE (elt))
  841.         {
  842.           /* We can ignore CLOBBERs.  */
  843.         case CLOBBER:
  844.           break;
  845.  
  846.         case SET:
  847.           /* Ignore SETs whose result isn't used but not those that
  848.          have side-effects.  */
  849.           if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
  850.           && ! side_effects_p (elt))
  851.         break;
  852.  
  853.           /* If we have already found a SET, this is a second one and
  854.          so we cannot combine with this insn.  */
  855.           if (set)
  856.         return 0;
  857.  
  858.           set = elt;
  859.           break;
  860.  
  861.         default:
  862.           /* Anything else means we can't combine.  */
  863.           return 0;
  864.         }
  865.     }
  866.  
  867.       if (set == 0
  868.       /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
  869.          so don't do anything with it.  */
  870.       || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
  871.     return 0;
  872.     }
  873.   else
  874.     return 0;
  875.  
  876.   if (set == 0)
  877.     return 0;
  878.  
  879.   set = expand_field_assignment (set);
  880.   src = SET_SRC (set), dest = SET_DEST (set);
  881.  
  882.   /* Don't eliminate a store in the stack pointer.  */
  883.   if (dest == stack_pointer_rtx
  884.       /* If we couldn't eliminate a field assignment, we can't combine.  */
  885.       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
  886.       /* Don't combine with an insn that sets a register to itself if it has
  887.      a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
  888.       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
  889.       /* Can't merge a function call.  */
  890.       || GET_CODE (src) == CALL
  891.       /* Don't eliminate a function call argument.  */
  892.       || (GET_CODE (i3) == CALL_INSN
  893.       && (find_reg_fusage (i3, USE, dest)
  894.           || (GET_CODE (dest) == REG
  895.           && REGNO (dest) < FIRST_PSEUDO_REGISTER
  896.           && global_regs[REGNO (dest)])))
  897.       /* Don't substitute into an incremented register.  */
  898.       || FIND_REG_INC_NOTE (i3, dest)
  899.       || (succ && FIND_REG_INC_NOTE (succ, dest))
  900.       /* Don't combine the end of a libcall into anything.  */
  901.       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
  902.       /* Make sure that DEST is not used after SUCC but before I3.  */
  903.       || (succ && ! all_adjacent
  904.       && reg_used_between_p (dest, succ, i3))
  905.       /* Make sure that the value that is to be substituted for the register
  906.      does not use any registers whose values alter in between.  However,
  907.      If the insns are adjacent, a use can't cross a set even though we
  908.      think it might (this can happen for a sequence of insns each setting
  909.      the same destination; reg_last_set of that register might point to
  910.      a NOTE).  If INSN has a REG_EQUIV note, the register is always
  911.      equivalent to the memory so the substitution is valid even if there
  912.      are intervening stores.  Also, don't move a volatile asm or
  913.      UNSPEC_VOLATILE across any other insns.  */
  914.       || (! all_adjacent
  915.       && (((GET_CODE (src) != MEM
  916.         || ! find_reg_note (insn, REG_EQUIV, src))
  917.            && use_crosses_set_p (src, INSN_CUID (insn)))
  918.           || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
  919.           || GET_CODE (src) == UNSPEC_VOLATILE))
  920.       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
  921.      better register allocation by not doing the combine.  */
  922.       || find_reg_note (i3, REG_NO_CONFLICT, dest)
  923.       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
  924.       /* Don't combine across a CALL_INSN, because that would possibly
  925.      change whether the life span of some REGs crosses calls or not,
  926.      and it is a pain to update that information.
  927.      Exception: if source is a constant, moving it later can't hurt.
  928.      Accept that special case, because it helps -fforce-addr a lot.  */
  929.       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
  930.     return 0;
  931.  
  932.   /* DEST must either be a REG or CC0.  */
  933.   if (GET_CODE (dest) == REG)
  934.     {
  935.       /* If register alignment is being enforced for multi-word items in all
  936.      cases except for parameters, it is possible to have a register copy
  937.      insn referencing a hard register that is not allowed to contain the
  938.      mode being copied and which would not be valid as an operand of most
  939.      insns.  Eliminate this problem by not combining with such an insn.
  940.  
  941.      Also, on some machines we don't want to extend the life of a hard
  942.      register.  */
  943.  
  944.       if (GET_CODE (src) == REG
  945.       && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
  946.            && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
  947.           /* Don't extend the life of a hard register unless it is
  948.          user variable (if we have few registers) or it can't
  949.          fit into the desired register (meaning something special
  950.          is going on).  */
  951.           || (REGNO (src) < FIRST_PSEUDO_REGISTER
  952.           && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
  953. #ifdef SMALL_REGISTER_CLASSES
  954.               || ! REG_USERVAR_P (src)
  955. #endif
  956.               ))))
  957.     return 0;
  958.     }
  959.   else if (GET_CODE (dest) != CC0)
  960.     return 0;
  961.  
  962.   /* Don't substitute for a register intended as a clobberable operand.
  963.      Similarly, don't substitute an expression containing a register that
  964.      will be clobbered in I3.  */
  965.   if (GET_CODE (PATTERN (i3)) == PARALLEL)
  966.     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
  967.       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
  968.       && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
  969.                        src)
  970.           || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
  971.     return 0;
  972.  
  973.   /* If INSN contains anything volatile, or is an `asm' (whether volatile
  974.      or not), reject, unless nothing volatile comes between it and I3,
  975.      with the exception of SUCC.  */
  976.  
  977.   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
  978.     for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
  979.       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
  980.       && p != succ && volatile_refs_p (PATTERN (p)))
  981.     return 0;
  982.  
  983.   /* If there are any volatile insns between INSN and I3, reject, because
  984.      they might affect machine state.  */
  985.  
  986.   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
  987.     if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
  988.     && p != succ && volatile_insn_p (PATTERN (p)))
  989.       return 0;
  990.  
  991.   /* If INSN or I2 contains an autoincrement or autodecrement,
  992.      make sure that register is not used between there and I3,
  993.      and not already used in I3 either.
  994.      Also insist that I3 not be a jump; if it were one
  995.      and the incremented register were spilled, we would lose.  */
  996.  
  997. #ifdef AUTO_INC_DEC
  998.   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  999.     if (REG_NOTE_KIND (link) == REG_INC
  1000.     && (GET_CODE (i3) == JUMP_INSN
  1001.         || reg_used_between_p (XEXP (link, 0), insn, i3)
  1002.         || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
  1003.       return 0;
  1004. #endif
  1005.  
  1006. #ifdef HAVE_cc0
  1007.   /* Don't combine an insn that follows a CC0-setting insn.
  1008.      An insn that uses CC0 must not be separated from the one that sets it.
  1009.      We do, however, allow I2 to follow a CC0-setting insn if that insn
  1010.      is passed as I1; in that case it will be deleted also.
  1011.      We also allow combining in this case if all the insns are adjacent
  1012.      because that would leave the two CC0 insns adjacent as well.
  1013.      It would be more logical to test whether CC0 occurs inside I1 or I2,
  1014.      but that would be much slower, and this ought to be equivalent.  */
  1015.  
  1016.   p = prev_nonnote_insn (insn);
  1017.   if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
  1018.       && ! all_adjacent)
  1019.     return 0;
  1020. #endif
  1021.  
  1022.   /* If we get here, we have passed all the tests and the combination is
  1023.      to be allowed.  */
  1024.  
  1025.   *pdest = dest;
  1026.   *psrc = src;
  1027.  
  1028.   return 1;
  1029. }
  1030.  
  1031. /* LOC is the location within I3 that contains its pattern or the component
  1032.    of a PARALLEL of the pattern.  We validate that it is valid for combining.
  1033.  
  1034.    One problem is if I3 modifies its output, as opposed to replacing it
  1035.    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
  1036.    so would produce an insn that is not equivalent to the original insns.
  1037.  
  1038.    Consider:
  1039.  
  1040.          (set (reg:DI 101) (reg:DI 100))
  1041.      (set (subreg:SI (reg:DI 101) 0) <foo>)
  1042.  
  1043.    This is NOT equivalent to:
  1044.  
  1045.          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
  1046.              (set (reg:DI 101) (reg:DI 100))])
  1047.  
  1048.    Not only does this modify 100 (in which case it might still be valid
  1049.    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100. 
  1050.  
  1051.    We can also run into a problem if I2 sets a register that I1
  1052.    uses and I1 gets directly substituted into I3 (not via I2).  In that
  1053.    case, we would be getting the wrong value of I2DEST into I3, so we
  1054.    must reject the combination.  This case occurs when I2 and I1 both
  1055.    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
  1056.    If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
  1057.    of a SET must prevent combination from occurring.
  1058.  
  1059.    On machines where SMALL_REGISTER_CLASSES is defined, we don't combine
  1060.    if the destination of a SET is a hard register that isn't a user
  1061.    variable.
  1062.  
  1063.    Before doing the above check, we first try to expand a field assignment
  1064.    into a set of logical operations.
  1065.  
  1066.    If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
  1067.    we place a register that is both set and used within I3.  If more than one
  1068.    such register is detected, we fail.
  1069.  
  1070.    Return 1 if the combination is valid, zero otherwise.  */
  1071.  
  1072. static int
  1073. combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
  1074.      rtx i3;
  1075.      rtx *loc;
  1076.      rtx i2dest;
  1077.      rtx i1dest;
  1078.      int i1_not_in_src;
  1079.      rtx *pi3dest_killed;
  1080. {
  1081.   rtx x = *loc;
  1082.  
  1083.   if (GET_CODE (x) == SET)
  1084.     {
  1085.       rtx set = expand_field_assignment (x);
  1086.       rtx dest = SET_DEST (set);
  1087.       rtx src = SET_SRC (set);
  1088.       rtx inner_dest = dest, inner_src = src;
  1089.  
  1090.       SUBST (*loc, set);
  1091.  
  1092.       while (GET_CODE (inner_dest) == STRICT_LOW_PART
  1093.          || GET_CODE (inner_dest) == SUBREG
  1094.          || GET_CODE (inner_dest) == ZERO_EXTRACT)
  1095.     inner_dest = XEXP (inner_dest, 0);
  1096.  
  1097.   /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
  1098.      was added.  */
  1099. #if 0
  1100.       while (GET_CODE (inner_src) == STRICT_LOW_PART
  1101.          || GET_CODE (inner_src) == SUBREG
  1102.          || GET_CODE (inner_src) == ZERO_EXTRACT)
  1103.     inner_src = XEXP (inner_src, 0);
  1104.  
  1105.       /* If it is better that two different modes keep two different pseudos,
  1106.      avoid combining them.  This avoids producing the following pattern
  1107.      on a 386:
  1108.       (set (subreg:SI (reg/v:QI 21) 0)
  1109.            (lshiftrt:SI (reg/v:SI 20)
  1110.                (const_int 24)))
  1111.      If that were made, reload could not handle the pair of
  1112.      reg 20/21, since it would try to get any GENERAL_REGS
  1113.      but some of them don't handle QImode.  */
  1114.  
  1115.       if (rtx_equal_p (inner_src, i2dest)
  1116.       && GET_CODE (inner_dest) == REG
  1117.       && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
  1118.     return 0;
  1119. #endif
  1120.  
  1121.       /* Check for the case where I3 modifies its output, as
  1122.      discussed above.  */
  1123.       if ((inner_dest != dest
  1124.        && (reg_overlap_mentioned_p (i2dest, inner_dest)
  1125.            || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
  1126.       /* This is the same test done in can_combine_p except that we
  1127.          allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
  1128.          CALL operation.  */
  1129.       || (GET_CODE (inner_dest) == REG
  1130.           && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
  1131.           && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
  1132.                     GET_MODE (inner_dest))
  1133. #ifdef SMALL_REGISTER_CLASSES
  1134.          || (GET_CODE (src) != CALL && ! REG_USERVAR_P (inner_dest))
  1135. #endif
  1136.           ))
  1137.       || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
  1138.     return 0;
  1139.  
  1140.       /* If DEST is used in I3, it is being killed in this insn,
  1141.      so record that for later. 
  1142.      Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
  1143.      STACK_POINTER_REGNUM, since these are always considered to be
  1144.      live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
  1145.       if (pi3dest_killed && GET_CODE (dest) == REG
  1146.       && reg_referenced_p (dest, PATTERN (i3))
  1147.       && REGNO (dest) != FRAME_POINTER_REGNUM
  1148. #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
  1149.       && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
  1150. #endif
  1151. #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
  1152.       && (REGNO (dest) != ARG_POINTER_REGNUM
  1153.           || ! fixed_regs [REGNO (dest)])
  1154. #endif
  1155.       && REGNO (dest) != STACK_POINTER_REGNUM)
  1156.     {
  1157.       if (*pi3dest_killed)
  1158.         return 0;
  1159.  
  1160.       *pi3dest_killed = dest;
  1161.     }
  1162.     }
  1163.  
  1164.   else if (GET_CODE (x) == PARALLEL)
  1165.     {
  1166.       int i;
  1167.  
  1168.       for (i = 0; i < XVECLEN (x, 0); i++)
  1169.     if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
  1170.                 i1_not_in_src, pi3dest_killed))
  1171.       return 0;
  1172.     }
  1173.  
  1174.   return 1;
  1175. }
  1176.  
  1177. /* Try to combine the insns I1 and I2 into I3.
  1178.    Here I1 and I2 appear earlier than I3.
  1179.    I1 can be zero; then we combine just I2 into I3.
  1180.  
  1181.    It we are combining three insns and the resulting insn is not recognized,
  1182.    try splitting it into two insns.  If that happens, I2 and I3 are retained
  1183.    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
  1184.    are pseudo-deleted.
  1185.  
  1186.    Return 0 if the combination does not work.  Then nothing is changed. 
  1187.    If we did the combination, return the insn at which combine should
  1188.    resume scanning.  */
  1189.  
  1190. static rtx
  1191. try_combine (i3, i2, i1)
  1192.      register rtx i3, i2, i1;
  1193. {
  1194.   /* New patterns for I3 and I3, respectively.  */
  1195.   rtx newpat, newi2pat = 0;
  1196.   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
  1197.   int added_sets_1, added_sets_2;
  1198.   /* Total number of SETs to put into I3.  */
  1199.   int total_sets;
  1200.   /* Nonzero is I2's body now appears in I3.  */
  1201.   int i2_is_used;
  1202.   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
  1203.   int insn_code_number, i2_code_number, other_code_number;
  1204.   /* Contains I3 if the destination of I3 is used in its source, which means
  1205.      that the old life of I3 is being killed.  If that usage is placed into
  1206.      I2 and not in I3, a REG_DEAD note must be made.  */
  1207.   rtx i3dest_killed = 0;
  1208.   /* SET_DEST and SET_SRC of I2 and I1.  */
  1209.   rtx i2dest, i2src, i1dest = 0, i1src = 0;
  1210.   /* PATTERN (I2), or a copy of it in certain cases.  */
  1211.   rtx i2pat;
  1212.   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
  1213.   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
  1214.   int i1_feeds_i3 = 0;
  1215.   /* Notes that must be added to REG_NOTES in I3 and I2.  */
  1216.   rtx new_i3_notes, new_i2_notes;
  1217.   /* Notes that we substituted I3 into I2 instead of the normal case.  */
  1218.   int i3_subst_into_i2 = 0;
  1219.   /* Notes that I1, I2 or I3 is a MULT operation.  */
  1220.   int have_mult = 0;
  1221.   /* Number of clobbers of SCRATCH we had to add.  */
  1222.   int i3_scratches = 0, i2_scratches = 0, other_scratches = 0;
  1223.  
  1224.   int maxreg;
  1225.   rtx temp;
  1226.   register rtx link;
  1227.   int i;
  1228.  
  1229.   /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
  1230.      This can occur when flow deletes an insn that it has merged into an
  1231.      auto-increment address.  We also can't do anything if I3 has a
  1232.      REG_LIBCALL note since we don't want to disrupt the contiguity of a
  1233.      libcall.  */
  1234.  
  1235.   if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
  1236.       || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
  1237.       || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
  1238.       || find_reg_note (i3, REG_LIBCALL, NULL_RTX))
  1239.     return 0;
  1240.  
  1241.   combine_attempts++;
  1242.  
  1243.   undobuf.num_undo = previous_num_undos = 0;
  1244.   undobuf.other_insn = 0;
  1245.  
  1246.   /* Save the current high-water-mark so we can free storage if we didn't
  1247.      accept this combination.  */
  1248.   undobuf.storage = (char *) oballoc (0);
  1249.  
  1250.   /* Reset the hard register usage information.  */
  1251.   CLEAR_HARD_REG_SET (newpat_used_regs);
  1252.  
  1253.   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
  1254.      code below, set I1 to be the earlier of the two insns.  */
  1255.   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
  1256.     temp = i1, i1 = i2, i2 = temp;
  1257.  
  1258.   added_links_insn = 0;
  1259.  
  1260.   /* First check for one important special-case that the code below will
  1261.      not handle.  Namely, the case where I1 is zero, I2 has multiple sets,
  1262.      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
  1263.      we may be able to replace that destination with the destination of I3.
  1264.      This occurs in the common code where we compute both a quotient and
  1265.      remainder into a structure, in which case we want to do the computation
  1266.      directly into the structure to avoid register-register copies.
  1267.  
  1268.      We make very conservative checks below and only try to handle the
  1269.      most common cases of this.  For example, we only handle the case
  1270.      where I2 and I3 are adjacent to avoid making difficult register
  1271.      usage tests.  */
  1272.  
  1273.   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
  1274.       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
  1275.       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
  1276. #ifdef SMALL_REGISTER_CLASSES
  1277.       && (GET_CODE (SET_DEST (PATTERN (i3))) != REG
  1278.       || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
  1279.       || REG_USERVAR_P (SET_DEST (PATTERN (i3))))
  1280. #endif
  1281.       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
  1282.       && GET_CODE (PATTERN (i2)) == PARALLEL
  1283.       && ! side_effects_p (SET_DEST (PATTERN (i3)))
  1284.       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
  1285.      below would need to check what is inside (and reg_overlap_mentioned_p
  1286.      doesn't support those codes anyway).  Don't allow those destinations;
  1287.      the resulting insn isn't likely to be recognized anyway.  */
  1288.       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
  1289.       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
  1290.       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
  1291.                     SET_DEST (PATTERN (i3)))
  1292.       && next_real_insn (i2) == i3)
  1293.     {
  1294.       rtx p2 = PATTERN (i2);
  1295.  
  1296.       /* Make sure that the destination of I3,
  1297.      which we are going to substitute into one output of I2,
  1298.      is not used within another output of I2.  We must avoid making this:
  1299.      (parallel [(set (mem (reg 69)) ...)
  1300.             (set (reg 69) ...)])
  1301.      which is not well-defined as to order of actions.
  1302.      (Besides, reload can't handle output reloads for this.)
  1303.  
  1304.      The problem can also happen if the dest of I3 is a memory ref,
  1305.      if another dest in I2 is an indirect memory ref.  */
  1306.       for (i = 0; i < XVECLEN (p2, 0); i++)
  1307.     if (GET_CODE (XVECEXP (p2, 0, i)) == SET
  1308.         && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
  1309.                     SET_DEST (XVECEXP (p2, 0, i))))
  1310.       break;
  1311.  
  1312.       if (i == XVECLEN (p2, 0))
  1313.     for (i = 0; i < XVECLEN (p2, 0); i++)
  1314.       if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
  1315.         {
  1316.           combine_merges++;
  1317.  
  1318.           subst_insn = i3;
  1319.           subst_low_cuid = INSN_CUID (i2);
  1320.  
  1321.           added_sets_2 = added_sets_1 = 0;
  1322.           i2dest = SET_SRC (PATTERN (i3));
  1323.  
  1324.           /* Replace the dest in I2 with our dest and make the resulting
  1325.          insn the new pattern for I3.  Then skip to where we
  1326.          validate the pattern.  Everything was set up above.  */
  1327.           SUBST (SET_DEST (XVECEXP (p2, 0, i)), 
  1328.              SET_DEST (PATTERN (i3)));
  1329.  
  1330.           newpat = p2;
  1331.           i3_subst_into_i2 = 1;
  1332.           goto validate_replacement;
  1333.         }
  1334.     }
  1335.  
  1336. #ifndef HAVE_cc0
  1337.   /* If we have no I1 and I2 looks like:
  1338.     (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
  1339.            (set Y OP)])
  1340.      make up a dummy I1 that is
  1341.     (set Y OP)
  1342.      and change I2 to be
  1343.         (set (reg:CC X) (compare:CC Y (const_int 0)))
  1344.  
  1345.      (We can ignore any trailing CLOBBERs.)
  1346.  
  1347.      This undoes a previous combination and allows us to match a branch-and-
  1348.      decrement insn.  */
  1349.  
  1350.   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
  1351.       && XVECLEN (PATTERN (i2), 0) >= 2
  1352.       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
  1353.       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
  1354.       == MODE_CC)
  1355.       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
  1356.       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
  1357.       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
  1358.       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
  1359.       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
  1360.               SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
  1361.     {
  1362.       for (i =  XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
  1363.     if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
  1364.       break;
  1365.  
  1366.       if (i == 1)
  1367.     {
  1368.       /* We make I1 with the same INSN_UID as I2.  This gives it
  1369.          the same INSN_CUID for value tracking.  Our fake I1 will
  1370.          never appear in the insn stream so giving it the same INSN_UID
  1371.          as I2 will not cause a problem.  */
  1372.  
  1373.       subst_prev_insn = i1
  1374.         = gen_rtx (INSN, VOIDmode, INSN_UID (i2), 0, i2,
  1375.                XVECEXP (PATTERN (i2), 0, 1), -1, 0, 0);
  1376.  
  1377.       SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
  1378.       SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
  1379.          SET_DEST (PATTERN (i1)));
  1380.     }
  1381.     }
  1382. #endif
  1383.  
  1384.   /* Verify that I2 and I1 are valid for combining.  */
  1385.   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
  1386.       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
  1387.     {
  1388.       undo_all ();
  1389.       return 0;
  1390.     }
  1391.  
  1392.   /* Record whether I2DEST is used in I2SRC and similarly for the other
  1393.      cases.  Knowing this will help in register status updating below.  */
  1394.   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
  1395.   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
  1396.   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
  1397.  
  1398.   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
  1399.      in I2SRC.  */
  1400.   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
  1401.  
  1402.   /* Ensure that I3's pattern can be the destination of combines.  */
  1403.   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
  1404.               i1 && i2dest_in_i1src && i1_feeds_i3,
  1405.               &i3dest_killed))
  1406.     {
  1407.       undo_all ();
  1408.       return 0;
  1409.     }
  1410.  
  1411.   /* See if any of the insns is a MULT operation.  Unless one is, we will
  1412.      reject a combination that is, since it must be slower.  Be conservative
  1413.      here.  */
  1414.   if (GET_CODE (i2src) == MULT
  1415.       || (i1 != 0 && GET_CODE (i1src) == MULT)
  1416.       || (GET_CODE (PATTERN (i3)) == SET
  1417.       && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
  1418.     have_mult = 1;
  1419.  
  1420.   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
  1421.      We used to do this EXCEPT in one case: I3 has a post-inc in an
  1422.      output operand.  However, that exception can give rise to insns like
  1423.          mov r3,(r3)+
  1424.      which is a famous insn on the PDP-11 where the value of r3 used as the
  1425.      source was model-dependent.  Avoid this sort of thing.  */
  1426.  
  1427. #if 0
  1428.   if (!(GET_CODE (PATTERN (i3)) == SET
  1429.     && GET_CODE (SET_SRC (PATTERN (i3))) == REG
  1430.     && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
  1431.     && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
  1432.         || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
  1433.     /* It's not the exception.  */
  1434. #endif
  1435. #ifdef AUTO_INC_DEC
  1436.     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
  1437.       if (REG_NOTE_KIND (link) == REG_INC
  1438.       && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
  1439.           || (i1 != 0
  1440.           && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
  1441.     {
  1442.       undo_all ();
  1443.       return 0;
  1444.     }
  1445. #endif
  1446.  
  1447.   /* See if the SETs in I1 or I2 need to be kept around in the merged
  1448.      instruction: whenever the value set there is still needed past I3.
  1449.      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
  1450.  
  1451.      For the SET in I1, we have two cases:  If I1 and I2 independently
  1452.      feed into I3, the set in I1 needs to be kept around if I1DEST dies
  1453.      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
  1454.      in I1 needs to be kept around unless I1DEST dies or is set in either
  1455.      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
  1456.      I1DEST.  If so, we know I1 feeds into I2.  */
  1457.  
  1458.   added_sets_2 = ! dead_or_set_p (i3, i2dest);
  1459.  
  1460.   added_sets_1
  1461.     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
  1462.            : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
  1463.  
  1464.   /* If the set in I2 needs to be kept around, we must make a copy of
  1465.      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
  1466.      PATTERN (I2), we are only substituting for the original I1DEST, not into
  1467.      an already-substituted copy.  This also prevents making self-referential
  1468.      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
  1469.      I2DEST.  */
  1470.  
  1471.   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
  1472.        ? gen_rtx (SET, VOIDmode, i2dest, i2src)
  1473.        : PATTERN (i2));
  1474.  
  1475.   if (added_sets_2)
  1476.     i2pat = copy_rtx (i2pat);
  1477.  
  1478.   combine_merges++;
  1479.  
  1480.   /* Substitute in the latest insn for the regs set by the earlier ones.  */
  1481.  
  1482.   maxreg = max_reg_num ();
  1483.  
  1484.   subst_insn = i3;
  1485.  
  1486.   /* It is possible that the source of I2 or I1 may be performing an
  1487.      unneeded operation, such as a ZERO_EXTEND of something that is known
  1488.      to have the high part zero.  Handle that case by letting subst look at
  1489.      the innermost one of them.
  1490.  
  1491.      Another way to do this would be to have a function that tries to
  1492.      simplify a single insn instead of merging two or more insns.  We don't
  1493.      do this because of the potential of infinite loops and because
  1494.      of the potential extra memory required.  However, doing it the way
  1495.      we are is a bit of a kludge and doesn't catch all cases.
  1496.  
  1497.      But only do this if -fexpensive-optimizations since it slows things down
  1498.      and doesn't usually win.  */
  1499.  
  1500.   if (flag_expensive_optimizations)
  1501.     {
  1502.       /* Pass pc_rtx so no substitutions are done, just simplifications.
  1503.      The cases that we are interested in here do not involve the few
  1504.      cases were is_replaced is checked.  */
  1505.       if (i1)
  1506.     {
  1507.       subst_low_cuid = INSN_CUID (i1);
  1508.       i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
  1509.     }
  1510.       else
  1511.     {
  1512.       subst_low_cuid = INSN_CUID (i2);
  1513.       i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
  1514.     }
  1515.  
  1516.       previous_num_undos = undobuf.num_undo;
  1517.     }
  1518.  
  1519. #ifndef HAVE_cc0
  1520.   /* Many machines that don't use CC0 have insns that can both perform an
  1521.      arithmetic operation and set the condition code.  These operations will
  1522.      be represented as a PARALLEL with the first element of the vector
  1523.      being a COMPARE of an arithmetic operation with the constant zero.
  1524.      The second element of the vector will set some pseudo to the result
  1525.      of the same arithmetic operation.  If we simplify the COMPARE, we won't
  1526.      match such a pattern and so will generate an extra insn.   Here we test
  1527.      for this case, where both the comparison and the operation result are
  1528.      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
  1529.      I2SRC.  Later we will make the PARALLEL that contains I2.  */
  1530.  
  1531.   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
  1532.       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
  1533.       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
  1534.       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
  1535.     {
  1536.       rtx *cc_use;
  1537.       enum machine_mode compare_mode;
  1538.  
  1539.       newpat = PATTERN (i3);
  1540.       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
  1541.  
  1542.       i2_is_used = 1;
  1543.  
  1544. #ifdef EXTRA_CC_MODES
  1545.       /* See if a COMPARE with the operand we substituted in should be done
  1546.      with the mode that is currently being used.  If not, do the same
  1547.      processing we do in `subst' for a SET; namely, if the destination
  1548.      is used only once, try to replace it with a register of the proper
  1549.      mode and also replace the COMPARE.  */
  1550.       if (undobuf.other_insn == 0
  1551.       && (cc_use = find_single_use (SET_DEST (newpat), i3,
  1552.                     &undobuf.other_insn))
  1553.       && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
  1554.                           i2src, const0_rtx))
  1555.           != GET_MODE (SET_DEST (newpat))))
  1556.     {
  1557.       int regno = REGNO (SET_DEST (newpat));
  1558.       rtx new_dest = gen_rtx (REG, compare_mode, regno);
  1559.  
  1560.       if (regno < FIRST_PSEUDO_REGISTER
  1561.           || (reg_n_sets[regno] == 1 && ! added_sets_2
  1562.           && ! REG_USERVAR_P (SET_DEST (newpat))))
  1563.         {
  1564.           if (regno >= FIRST_PSEUDO_REGISTER)
  1565.         SUBST (regno_reg_rtx[regno], new_dest);
  1566.  
  1567.           SUBST (SET_DEST (newpat), new_dest);
  1568.           SUBST (XEXP (*cc_use, 0), new_dest);
  1569.           SUBST (SET_SRC (newpat),
  1570.              gen_rtx_combine (COMPARE, compare_mode,
  1571.                       i2src, const0_rtx));
  1572.         }
  1573.       else
  1574.         undobuf.other_insn = 0;
  1575.     }
  1576. #endif      
  1577.     }
  1578.   else
  1579. #endif
  1580.     {
  1581.       n_occurrences = 0;        /* `subst' counts here */
  1582.  
  1583.       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
  1584.      need to make a unique copy of I2SRC each time we substitute it
  1585.      to avoid self-referential rtl.  */
  1586.  
  1587.       subst_low_cuid = INSN_CUID (i2);
  1588.       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
  1589.               ! i1_feeds_i3 && i1dest_in_i1src);
  1590.       previous_num_undos = undobuf.num_undo;
  1591.  
  1592.       /* Record whether i2's body now appears within i3's body.  */
  1593.       i2_is_used = n_occurrences;
  1594.     }
  1595.  
  1596.   /* If we already got a failure, don't try to do more.  Otherwise,
  1597.      try to substitute in I1 if we have it.  */
  1598.  
  1599.   if (i1 && GET_CODE (newpat) != CLOBBER)
  1600.     {
  1601.       /* Before we can do this substitution, we must redo the test done
  1602.      above (see detailed comments there) that ensures  that I1DEST
  1603.      isn't mentioned in any SETs in NEWPAT that are field assignments. */
  1604.  
  1605.       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
  1606.                   0, NULL_PTR))
  1607.     {
  1608.       undo_all ();
  1609.       return 0;
  1610.     }
  1611.  
  1612.       n_occurrences = 0;
  1613.       subst_low_cuid = INSN_CUID (i1);
  1614.       newpat = subst (newpat, i1dest, i1src, 0, 0);
  1615.       previous_num_undos = undobuf.num_undo;
  1616.     }
  1617.  
  1618.   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
  1619.      to count all the ways that I2SRC and I1SRC can be used.  */
  1620.   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
  1621.        && i2_is_used + added_sets_2 > 1)
  1622.       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
  1623.       && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
  1624.           > 1))
  1625.       /* Fail if we tried to make a new register (we used to abort, but there's
  1626.      really no reason to).  */
  1627.       || max_reg_num () != maxreg
  1628.       /* Fail if we couldn't do something and have a CLOBBER.  */
  1629.       || GET_CODE (newpat) == CLOBBER
  1630.       /* Fail if this new pattern is a MULT and we didn't have one before
  1631.      at the outer level.  */
  1632.       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
  1633.       && ! have_mult))
  1634.     {
  1635.       undo_all ();
  1636.       return 0;
  1637.     }
  1638.  
  1639.   /* If the actions of the earlier insns must be kept
  1640.      in addition to substituting them into the latest one,
  1641.      we must make a new PARALLEL for the latest insn
  1642.      to hold additional the SETs.  */
  1643.  
  1644.   if (added_sets_1 || added_sets_2)
  1645.     {
  1646.       combine_extras++;
  1647.  
  1648.       if (GET_CODE (newpat) == PARALLEL)
  1649.     {
  1650.       rtvec old = XVEC (newpat, 0);
  1651.       total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
  1652.       newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  1653.       bcopy ((char *) &old->elem[0], (char *) &XVECEXP (newpat, 0, 0),
  1654.          sizeof (old->elem[0]) * old->num_elem);
  1655.     }
  1656.       else
  1657.     {
  1658.       rtx old = newpat;
  1659.       total_sets = 1 + added_sets_1 + added_sets_2;
  1660.       newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  1661.       XVECEXP (newpat, 0, 0) = old;
  1662.     }
  1663.  
  1664.      if (added_sets_1)
  1665.        XVECEXP (newpat, 0, --total_sets)
  1666.      = (GET_CODE (PATTERN (i1)) == PARALLEL
  1667.         ? gen_rtx (SET, VOIDmode, i1dest, i1src) : PATTERN (i1));
  1668.  
  1669.      if (added_sets_2)
  1670.     {
  1671.       /* If there is no I1, use I2's body as is.  We used to also not do
  1672.          the subst call below if I2 was substituted into I3,
  1673.          but that could lose a simplification.  */
  1674.       if (i1 == 0)
  1675.         XVECEXP (newpat, 0, --total_sets) = i2pat;
  1676.       else
  1677.         /* See comment where i2pat is assigned.  */
  1678.         XVECEXP (newpat, 0, --total_sets)
  1679.           = subst (i2pat, i1dest, i1src, 0, 0);
  1680.     }
  1681.     }
  1682.  
  1683.   /* We come here when we are replacing a destination in I2 with the
  1684.      destination of I3.  */
  1685.  validate_replacement:
  1686.  
  1687.   /* Note which hard regs this insn has as inputs.  */
  1688.   mark_used_regs_combine (newpat);
  1689.  
  1690.   /* Is the result of combination a valid instruction?  */
  1691.   insn_code_number
  1692.     = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
  1693.  
  1694.   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
  1695.      the second SET's destination is a register that is unused.  In that case,
  1696.      we just need the first SET.   This can occur when simplifying a divmod
  1697.      insn.  We *must* test for this case here because the code below that
  1698.      splits two independent SETs doesn't handle this case correctly when it
  1699.      updates the register status.  Also check the case where the first
  1700.      SET's destination is unused.  That would not cause incorrect code, but
  1701.      does cause an unneeded insn to remain.  */
  1702.  
  1703.   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
  1704.       && XVECLEN (newpat, 0) == 2
  1705.       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
  1706.       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
  1707.       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
  1708.       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
  1709.       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
  1710.       && asm_noperands (newpat) < 0)
  1711.     {
  1712.       newpat = XVECEXP (newpat, 0, 0);
  1713.       insn_code_number
  1714.     = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
  1715.     }
  1716.  
  1717.   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
  1718.        && XVECLEN (newpat, 0) == 2
  1719.        && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
  1720.        && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
  1721.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
  1722.        && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
  1723.        && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
  1724.        && asm_noperands (newpat) < 0)
  1725.     {
  1726.       newpat = XVECEXP (newpat, 0, 1);
  1727.       insn_code_number
  1728.     = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
  1729.     }
  1730.  
  1731.   /* If we were combining three insns and the result is a simple SET
  1732.      with no ASM_OPERANDS that wasn't recognized, try to split it into two
  1733.      insns.  There are two ways to do this.  It can be split using a 
  1734.      machine-specific method (like when you have an addition of a large
  1735.      constant) or by combine in the function find_split_point.  */
  1736.  
  1737.   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
  1738.       && asm_noperands (newpat) < 0)
  1739.     {
  1740.       rtx m_split, *split;
  1741.       rtx ni2dest = i2dest;
  1742.  
  1743.       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
  1744.      use I2DEST as a scratch register will help.  In the latter case,
  1745.      convert I2DEST to the mode of the source of NEWPAT if we can.  */
  1746.  
  1747.       m_split = split_insns (newpat, i3);
  1748.  
  1749.       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
  1750.      inputs of NEWPAT.  */
  1751.  
  1752.       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
  1753.      possible to try that as a scratch reg.  This would require adding
  1754.      more code to make it work though.  */
  1755.  
  1756.       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
  1757.     {
  1758.       /* If I2DEST is a hard register or the only use of a pseudo,
  1759.          we can change its mode.  */
  1760.       if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
  1761.           && GET_MODE (SET_DEST (newpat)) != VOIDmode
  1762.           && GET_CODE (i2dest) == REG
  1763.           && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
  1764.           || (reg_n_sets[REGNO (i2dest)] == 1 && ! added_sets_2
  1765.               && ! REG_USERVAR_P (i2dest))))
  1766.         ni2dest = gen_rtx (REG, GET_MODE (SET_DEST (newpat)),
  1767.                    REGNO (i2dest));
  1768.  
  1769.       m_split = split_insns (gen_rtx (PARALLEL, VOIDmode,
  1770.                       gen_rtvec (2, newpat,
  1771.                              gen_rtx (CLOBBER,
  1772.                                   VOIDmode,
  1773.                                   ni2dest))),
  1774.                  i3);
  1775.     }
  1776.  
  1777.       if (m_split && GET_CODE (m_split) == SEQUENCE
  1778.       && XVECLEN (m_split, 0) == 2
  1779.       && (next_real_insn (i2) == i3
  1780.           || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
  1781.                       INSN_CUID (i2))))
  1782.     {
  1783.       rtx i2set, i3set;
  1784.       rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
  1785.       newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
  1786.  
  1787.       i3set = single_set (XVECEXP (m_split, 0, 1));
  1788.       i2set = single_set (XVECEXP (m_split, 0, 0));
  1789.  
  1790.       /* In case we changed the mode of I2DEST, replace it in the
  1791.          pseudo-register table here.  We can't do it above in case this
  1792.          code doesn't get executed and we do a split the other way.  */
  1793.  
  1794.       if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
  1795.         SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
  1796.  
  1797.       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes,
  1798.                           &i2_scratches);
  1799.  
  1800.       /* If I2 or I3 has multiple SETs, we won't know how to track
  1801.          register status, so don't use these insns.  */
  1802.  
  1803.       if (i2_code_number >= 0 && i2set && i3set)
  1804.         insn_code_number = recog_for_combine (&newi3pat, i3, &new_i3_notes,
  1805.                           &i3_scratches); 
  1806.       if (insn_code_number >= 0)
  1807.         newpat = newi3pat;
  1808.  
  1809.       /* It is possible that both insns now set the destination of I3.
  1810.          If so, we must show an extra use of it.  */
  1811.  
  1812.       if (insn_code_number >= 0 && GET_CODE (SET_DEST (i3set)) == REG
  1813.           && GET_CODE (SET_DEST (i2set)) == REG
  1814.           && REGNO (SET_DEST (i3set)) == REGNO (SET_DEST (i2set)))
  1815.         reg_n_sets[REGNO (SET_DEST (i2set))]++;
  1816.     }
  1817.  
  1818.       /* If we can split it and use I2DEST, go ahead and see if that
  1819.      helps things be recognized.  Verify that none of the registers
  1820.      are set between I2 and I3.  */
  1821.       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
  1822. #ifdef HAVE_cc0
  1823.       && GET_CODE (i2dest) == REG
  1824. #endif
  1825.       /* We need I2DEST in the proper mode.  If it is a hard register
  1826.          or the only use of a pseudo, we can change its mode.  */
  1827.       && (GET_MODE (*split) == GET_MODE (i2dest)
  1828.           || GET_MODE (*split) == VOIDmode
  1829.           || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
  1830.           || (reg_n_sets[REGNO (i2dest)] == 1 && ! added_sets_2
  1831.           && ! REG_USERVAR_P (i2dest)))
  1832.       && (next_real_insn (i2) == i3
  1833.           || ! use_crosses_set_p (*split, INSN_CUID (i2)))
  1834.       /* We can't overwrite I2DEST if its value is still used by
  1835.          NEWPAT.  */
  1836.       && ! reg_referenced_p (i2dest, newpat))
  1837.     {
  1838.       rtx newdest = i2dest;
  1839.       enum rtx_code split_code = GET_CODE (*split);
  1840.       enum machine_mode split_mode = GET_MODE (*split);
  1841.  
  1842.       /* Get NEWDEST as a register in the proper mode.  We have already
  1843.          validated that we can do this.  */
  1844.       if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
  1845.         {
  1846.           newdest = gen_rtx (REG, split_mode, REGNO (i2dest));
  1847.  
  1848.           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
  1849.         SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
  1850.         }
  1851.  
  1852.       /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
  1853.          an ASHIFT.  This can occur if it was inside a PLUS and hence
  1854.          appeared to be a memory address.  This is a kludge.  */
  1855.       if (split_code == MULT
  1856.           && GET_CODE (XEXP (*split, 1)) == CONST_INT
  1857.           && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
  1858.         {
  1859.           SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
  1860.                           XEXP (*split, 0), GEN_INT (i)));
  1861.           /* Update split_code because we may not have a multiply
  1862.          anymore.  */
  1863.           split_code = GET_CODE (*split);
  1864.         }
  1865.  
  1866. #ifdef INSN_SCHEDULING
  1867.       /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
  1868.          be written as a ZERO_EXTEND.  */
  1869.       if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
  1870.         SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
  1871.                         XEXP (*split, 0)));
  1872. #endif
  1873.  
  1874.       newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
  1875.       SUBST (*split, newdest);
  1876.       i2_code_number
  1877.         = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
  1878.  
  1879.       /* If the split point was a MULT and we didn't have one before,
  1880.          don't use one now.  */
  1881.       if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
  1882.         insn_code_number
  1883.           = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
  1884.     }
  1885.     }
  1886.  
  1887.   /* Check for a case where we loaded from memory in a narrow mode and
  1888.      then sign extended it, but we need both registers.  In that case,
  1889.      we have a PARALLEL with both loads from the same memory location.
  1890.      We can split this into a load from memory followed by a register-register
  1891.      copy.  This saves at least one insn, more if register allocation can
  1892.      eliminate the copy.
  1893.  
  1894.      We cannot do this if the destination of the second assignment is
  1895.      a register that we have already assumed is zero-extended.  Similarly
  1896.      for a SUBREG of such a register.  */
  1897.  
  1898.   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
  1899.        && GET_CODE (newpat) == PARALLEL
  1900.        && XVECLEN (newpat, 0) == 2
  1901.        && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
  1902.        && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
  1903.        && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
  1904.        && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
  1905.                XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
  1906.        && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
  1907.                    INSN_CUID (i2))
  1908.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
  1909.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
  1910.        && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
  1911.          (GET_CODE (temp) == REG
  1912.           && reg_nonzero_bits[REGNO (temp)] != 0
  1913.           && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
  1914.           && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
  1915.           && (reg_nonzero_bits[REGNO (temp)]
  1916.               != GET_MODE_MASK (word_mode))))
  1917.        && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
  1918.          && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
  1919.              (GET_CODE (temp) == REG
  1920.               && reg_nonzero_bits[REGNO (temp)] != 0
  1921.               && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
  1922.               && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
  1923.               && (reg_nonzero_bits[REGNO (temp)]
  1924.               != GET_MODE_MASK (word_mode)))))
  1925.        && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
  1926.                      SET_SRC (XVECEXP (newpat, 0, 1)))
  1927.        && ! find_reg_note (i3, REG_UNUSED,
  1928.                    SET_DEST (XVECEXP (newpat, 0, 0))))
  1929.     {
  1930.       rtx ni2dest;
  1931.  
  1932.       newi2pat = XVECEXP (newpat, 0, 0);
  1933.       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
  1934.       newpat = XVECEXP (newpat, 0, 1);
  1935.       SUBST (SET_SRC (newpat),
  1936.          gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
  1937.       i2_code_number
  1938.     = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
  1939.  
  1940.       if (i2_code_number >= 0)
  1941.     insn_code_number
  1942.       = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
  1943.  
  1944.       if (insn_code_number >= 0)
  1945.     {
  1946.       rtx insn;
  1947.       rtx link;
  1948.  
  1949.       /* If we will be able to accept this, we have made a change to the
  1950.          destination of I3.  This can invalidate a LOG_LINKS pointing
  1951.          to I3.  No other part of combine.c makes such a transformation.
  1952.  
  1953.          The new I3 will have a destination that was previously the
  1954.          destination of I1 or I2 and which was used in i2 or I3.  Call
  1955.          distribute_links to make a LOG_LINK from the next use of
  1956.          that destination.  */
  1957.  
  1958.       PATTERN (i3) = newpat;
  1959.       distribute_links (gen_rtx (INSN_LIST, VOIDmode, i3, NULL_RTX));
  1960.  
  1961.       /* I3 now uses what used to be its destination and which is
  1962.          now I2's destination.  That means we need a LOG_LINK from
  1963.          I3 to I2.  But we used to have one, so we still will.
  1964.  
  1965.          However, some later insn might be using I2's dest and have
  1966.          a LOG_LINK pointing at I3.  We must remove this link.
  1967.          The simplest way to remove the link is to point it at I1,
  1968.          which we know will be a NOTE.  */
  1969.  
  1970.       for (insn = NEXT_INSN (i3);
  1971.            insn && (this_basic_block == n_basic_blocks - 1
  1972.             || insn != basic_block_head[this_basic_block + 1]);
  1973.            insn = NEXT_INSN (insn))
  1974.         {
  1975.           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  1976.           && reg_referenced_p (ni2dest, PATTERN (insn)))
  1977.         {
  1978.           for (link = LOG_LINKS (insn); link;
  1979.                link = XEXP (link, 1))
  1980.             if (XEXP (link, 0) == i3)
  1981.               XEXP (link, 0) = i1;
  1982.  
  1983.           break;
  1984.         }
  1985.         }
  1986.     }
  1987.     }
  1988.         
  1989.   /* Similarly, check for a case where we have a PARALLEL of two independent
  1990.      SETs but we started with three insns.  In this case, we can do the sets
  1991.      as two separate insns.  This case occurs when some SET allows two
  1992.      other insns to combine, but the destination of that SET is still live.  */
  1993.  
  1994.   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
  1995.        && GET_CODE (newpat) == PARALLEL
  1996.        && XVECLEN (newpat, 0) == 2
  1997.        && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
  1998.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
  1999.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
  2000.        && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
  2001.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
  2002.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
  2003.        && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
  2004.                    INSN_CUID (i2))
  2005.        /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
  2006.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
  2007.        && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
  2008.        && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
  2009.                   XVECEXP (newpat, 0, 0))
  2010.        && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
  2011.                   XVECEXP (newpat, 0, 1)))
  2012.     {
  2013.       newi2pat = XVECEXP (newpat, 0, 1);
  2014.       newpat = XVECEXP (newpat, 0, 0);
  2015.  
  2016.       i2_code_number
  2017.     = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
  2018.  
  2019.       if (i2_code_number >= 0)
  2020.     insn_code_number
  2021.       = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
  2022.     }
  2023.  
  2024.   /* If it still isn't recognized, fail and change things back the way they
  2025.      were.  */
  2026.   if ((insn_code_number < 0
  2027.        /* Is the result a reasonable ASM_OPERANDS?  */
  2028.        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
  2029.     {
  2030.       undo_all ();
  2031.       return 0;
  2032.     }
  2033.  
  2034.   /* If we had to change another insn, make sure it is valid also.  */
  2035.   if (undobuf.other_insn)
  2036.     {
  2037.       rtx other_pat = PATTERN (undobuf.other_insn);
  2038.       rtx new_other_notes;
  2039.       rtx note, next;
  2040.  
  2041.       CLEAR_HARD_REG_SET (newpat_used_regs);
  2042.  
  2043.       other_code_number
  2044.     = recog_for_combine (&other_pat, undobuf.other_insn,
  2045.                  &new_other_notes, &other_scratches);
  2046.  
  2047.       if (other_code_number < 0 && ! check_asm_operands (other_pat))
  2048.     {
  2049.       undo_all ();
  2050.       return 0;
  2051.     }
  2052.  
  2053.       PATTERN (undobuf.other_insn) = other_pat;
  2054.  
  2055.       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
  2056.      are still valid.  Then add any non-duplicate notes added by
  2057.      recog_for_combine.  */
  2058.       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
  2059.     {
  2060.       next = XEXP (note, 1);
  2061.  
  2062.       if (REG_NOTE_KIND (note) == REG_UNUSED
  2063.           && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
  2064.         {
  2065.           if (GET_CODE (XEXP (note, 0)) == REG)
  2066.         reg_n_deaths[REGNO (XEXP (note, 0))]--;
  2067.  
  2068.           remove_note (undobuf.other_insn, note);
  2069.         }
  2070.     }
  2071.  
  2072.       for (note = new_other_notes; note; note = XEXP (note, 1))
  2073.     if (GET_CODE (XEXP (note, 0)) == REG)
  2074.       reg_n_deaths[REGNO (XEXP (note, 0))]++;
  2075.  
  2076.       distribute_notes (new_other_notes, undobuf.other_insn,
  2077.             undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
  2078.     }
  2079.  
  2080.   /* We now know that we can do this combination.  Merge the insns and 
  2081.      update the status of registers and LOG_LINKS.  */
  2082.  
  2083.   {
  2084.     rtx i3notes, i2notes, i1notes = 0;
  2085.     rtx i3links, i2links, i1links = 0;
  2086.     rtx midnotes = 0;
  2087.     register int regno;
  2088.     /* Compute which registers we expect to eliminate.  */
  2089.     rtx elim_i2 = (newi2pat || i2dest_in_i2src || i2dest_in_i1src
  2090.            ? 0 : i2dest);
  2091.     rtx elim_i1 = i1 == 0 || i1dest_in_i1src ? 0 : i1dest;
  2092.  
  2093.     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
  2094.        clear them.  */
  2095.     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
  2096.     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
  2097.     if (i1)
  2098.       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
  2099.  
  2100.     /* Ensure that we do not have something that should not be shared but
  2101.        occurs multiple times in the new insns.  Check this by first
  2102.        resetting all the `used' flags and then copying anything is shared.  */
  2103.  
  2104.     reset_used_flags (i3notes);
  2105.     reset_used_flags (i2notes);
  2106.     reset_used_flags (i1notes);
  2107.     reset_used_flags (newpat);
  2108.     reset_used_flags (newi2pat);
  2109.     if (undobuf.other_insn)
  2110.       reset_used_flags (PATTERN (undobuf.other_insn));
  2111.  
  2112.     i3notes = copy_rtx_if_shared (i3notes);
  2113.     i2notes = copy_rtx_if_shared (i2notes);
  2114.     i1notes = copy_rtx_if_shared (i1notes);
  2115.     newpat = copy_rtx_if_shared (newpat);
  2116.     newi2pat = copy_rtx_if_shared (newi2pat);
  2117.     if (undobuf.other_insn)
  2118.       reset_used_flags (PATTERN (undobuf.other_insn));
  2119.  
  2120.     INSN_CODE (i3) = insn_code_number;
  2121.     PATTERN (i3) = newpat;
  2122.     if (undobuf.other_insn)
  2123.       INSN_CODE (undobuf.other_insn) = other_code_number;
  2124.  
  2125.     /* We had one special case above where I2 had more than one set and
  2126.        we replaced a destination of one of those sets with the destination
  2127.        of I3.  In that case, we have to update LOG_LINKS of insns later
  2128.        in this basic block.  Note that this (expensive) case is rare.
  2129.  
  2130.        Also, in this case, we must pretend that all REG_NOTEs for I2
  2131.        actually came from I3, so that REG_UNUSED notes from I2 will be
  2132.        properly handled.  */
  2133.  
  2134.     if (i3_subst_into_i2)
  2135.       {
  2136.     for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
  2137.       if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
  2138.           && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
  2139.           && ! find_reg_note (i2, REG_UNUSED,
  2140.                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
  2141.         for (temp = NEXT_INSN (i2);
  2142.          temp && (this_basic_block == n_basic_blocks - 1
  2143.               || basic_block_head[this_basic_block] != temp);
  2144.          temp = NEXT_INSN (temp))
  2145.           if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
  2146.         for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
  2147.           if (XEXP (link, 0) == i2)
  2148.             XEXP (link, 0) = i3;
  2149.  
  2150.     if (i3notes)
  2151.       {
  2152.         rtx link = i3notes;
  2153.         while (XEXP (link, 1))
  2154.           link = XEXP (link, 1);
  2155.         XEXP (link, 1) = i2notes;
  2156.       }
  2157.     else
  2158.       i3notes = i2notes;
  2159.     i2notes = 0;
  2160.       }
  2161.  
  2162.     LOG_LINKS (i3) = 0;
  2163.     REG_NOTES (i3) = 0;
  2164.     LOG_LINKS (i2) = 0;
  2165.     REG_NOTES (i2) = 0;
  2166.  
  2167.     if (newi2pat)
  2168.       {
  2169.     INSN_CODE (i2) = i2_code_number;
  2170.     PATTERN (i2) = newi2pat;
  2171.       }
  2172.     else
  2173.       {
  2174.     PUT_CODE (i2, NOTE);
  2175.     NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
  2176.     NOTE_SOURCE_FILE (i2) = 0;
  2177.       }
  2178.  
  2179.     if (i1)
  2180.       {
  2181.     LOG_LINKS (i1) = 0;
  2182.     REG_NOTES (i1) = 0;
  2183.     PUT_CODE (i1, NOTE);
  2184.     NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
  2185.     NOTE_SOURCE_FILE (i1) = 0;
  2186.       }
  2187.  
  2188.     /* Get death notes for everything that is now used in either I3 or
  2189.        I2 and used to die in a previous insn.  */
  2190.  
  2191.     move_deaths (newpat, i1 ? INSN_CUID (i1) : INSN_CUID (i2), i3, &midnotes);
  2192.     if (newi2pat)
  2193.       move_deaths (newi2pat, INSN_CUID (i1), i2, &midnotes);
  2194.  
  2195.     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
  2196.     if (i3notes)
  2197.       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
  2198.             elim_i2, elim_i1);
  2199.     if (i2notes)
  2200.       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
  2201.             elim_i2, elim_i1);
  2202.     if (i1notes)
  2203.       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
  2204.             elim_i2, elim_i1);
  2205.     if (midnotes)
  2206.       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
  2207.             elim_i2, elim_i1);
  2208.  
  2209.     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
  2210.        know these are REG_UNUSED and want them to go to the desired insn,
  2211.        so we always pass it as i3.  We have not counted the notes in 
  2212.        reg_n_deaths yet, so we need to do so now.  */
  2213.  
  2214.     if (newi2pat && new_i2_notes)
  2215.       {
  2216.     for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
  2217.       if (GET_CODE (XEXP (temp, 0)) == REG)
  2218.         reg_n_deaths[REGNO (XEXP (temp, 0))]++;
  2219.     
  2220.     distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
  2221.       }
  2222.  
  2223.     if (new_i3_notes)
  2224.       {
  2225.     for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
  2226.       if (GET_CODE (XEXP (temp, 0)) == REG)
  2227.         reg_n_deaths[REGNO (XEXP (temp, 0))]++;
  2228.     
  2229.     distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
  2230.       }
  2231.  
  2232.     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
  2233.        put a REG_DEAD note for it somewhere.  Similarly for I2 and I1.
  2234.        Show an additional death due to the REG_DEAD note we make here.  If
  2235.        we discard it in distribute_notes, we will decrement it again.  */
  2236.  
  2237.     if (i3dest_killed)
  2238.       {
  2239.     if (GET_CODE (i3dest_killed) == REG)
  2240.       reg_n_deaths[REGNO (i3dest_killed)]++;
  2241.  
  2242.     distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i3dest_killed,
  2243.                    NULL_RTX),
  2244.               NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
  2245.               NULL_RTX, NULL_RTX);
  2246.       }
  2247.  
  2248.     /* For I2 and I1, we have to be careful.  If NEWI2PAT exists and sets
  2249.        I2DEST or I1DEST, the death must be somewhere before I2, not I3.  If
  2250.        we passed I3 in that case, it might delete I2.  */
  2251.  
  2252.     if (i2dest_in_i2src)
  2253.       {
  2254.     if (GET_CODE (i2dest) == REG)
  2255.       reg_n_deaths[REGNO (i2dest)]++;
  2256.  
  2257.     if (newi2pat && reg_set_p (i2dest, newi2pat))
  2258.       distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
  2259.                 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
  2260.     else
  2261.       distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
  2262.                 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
  2263.                 NULL_RTX, NULL_RTX);
  2264.       }
  2265.  
  2266.     if (i1dest_in_i1src)
  2267.       {
  2268.     if (GET_CODE (i1dest) == REG)
  2269.       reg_n_deaths[REGNO (i1dest)]++;
  2270.  
  2271.     if (newi2pat && reg_set_p (i1dest, newi2pat))
  2272.       distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
  2273.                 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
  2274.     else
  2275.       distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
  2276.                 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
  2277.                 NULL_RTX, NULL_RTX);
  2278.       }
  2279.  
  2280.     distribute_links (i3links);
  2281.     distribute_links (i2links);
  2282.     distribute_links (i1links);
  2283.  
  2284.     if (GET_CODE (i2dest) == REG)
  2285.       {
  2286.     rtx link;
  2287.     rtx i2_insn = 0, i2_val = 0, set;
  2288.  
  2289.     /* The insn that used to set this register doesn't exist, and
  2290.        this life of the register may not exist either.  See if one of
  2291.        I3's links points to an insn that sets I2DEST.  If it does, 
  2292.        that is now the last known value for I2DEST. If we don't update
  2293.        this and I2 set the register to a value that depended on its old
  2294.        contents, we will get confused.  If this insn is used, thing
  2295.        will be set correctly in combine_instructions.  */
  2296.  
  2297.     for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
  2298.       if ((set = single_set (XEXP (link, 0))) != 0
  2299.           && rtx_equal_p (i2dest, SET_DEST (set)))
  2300.         i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
  2301.  
  2302.     record_value_for_reg (i2dest, i2_insn, i2_val);
  2303.  
  2304.     /* If the reg formerly set in I2 died only once and that was in I3,
  2305.        zero its use count so it won't make `reload' do any work.  */
  2306.     if (! added_sets_2 && newi2pat == 0 && ! i2dest_in_i2src)
  2307.       {
  2308.         regno = REGNO (i2dest);
  2309.         reg_n_sets[regno]--;
  2310.         if (reg_n_sets[regno] == 0
  2311.         && ! (basic_block_live_at_start[0][regno / REGSET_ELT_BITS]
  2312.               & ((REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS))))
  2313.           reg_n_refs[regno] = 0;
  2314.       }
  2315.       }
  2316.  
  2317.     if (i1 && GET_CODE (i1dest) == REG)
  2318.       {
  2319.     rtx link;
  2320.     rtx i1_insn = 0, i1_val = 0, set;
  2321.  
  2322.     for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
  2323.       if ((set = single_set (XEXP (link, 0))) != 0
  2324.           && rtx_equal_p (i1dest, SET_DEST (set)))
  2325.         i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
  2326.  
  2327.     record_value_for_reg (i1dest, i1_insn, i1_val);
  2328.  
  2329.     regno = REGNO (i1dest);
  2330.     if (! added_sets_1 && ! i1dest_in_i1src)
  2331.       {
  2332.         reg_n_sets[regno]--;
  2333.         if (reg_n_sets[regno] == 0
  2334.         && ! (basic_block_live_at_start[0][regno / REGSET_ELT_BITS]
  2335.               & ((REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS))))
  2336.           reg_n_refs[regno] = 0;
  2337.       }
  2338.       }
  2339.  
  2340.     /* Update reg_nonzero_bits et al for any changes that may have been made
  2341.        to this insn.  */
  2342.  
  2343.     note_stores (newpat, set_nonzero_bits_and_sign_copies);
  2344.     if (newi2pat)
  2345.       note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
  2346.  
  2347.     /* If we added any (clobber (scratch)), add them to the max for a
  2348.        block.  This is a very pessimistic calculation, since we might
  2349.        have had them already and this might not be the worst block, but
  2350.        it's not worth doing any better.  */
  2351.     max_scratch += i3_scratches + i2_scratches + other_scratches;
  2352.  
  2353.     /* If I3 is now an unconditional jump, ensure that it has a 
  2354.        BARRIER following it since it may have initially been a
  2355.        conditional jump.  It may also be the last nonnote insn.  */
  2356.  
  2357.     if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
  2358.     && ((temp = next_nonnote_insn (i3)) == NULL_RTX
  2359.         || GET_CODE (temp) != BARRIER))
  2360.       emit_barrier_after (i3);
  2361.   }
  2362.  
  2363.   combine_successes++;
  2364.  
  2365.   /* Clear this here, so that subsequent get_last_value calls are not
  2366.      affected.  */
  2367.   subst_prev_insn = NULL_RTX;
  2368.  
  2369.   if (added_links_insn
  2370.       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
  2371.       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
  2372.     return added_links_insn;
  2373.   else
  2374.     return newi2pat ? i2 : i3;
  2375. }
  2376.  
  2377. /* Undo all the modifications recorded in undobuf.  */
  2378.  
  2379. static void
  2380. undo_all ()
  2381. {
  2382.   register int i;
  2383.   if (undobuf.num_undo > MAX_UNDO)
  2384.     undobuf.num_undo = MAX_UNDO;
  2385.   for (i = undobuf.num_undo - 1; i >= 0; i--)
  2386.     {
  2387.       if (undobuf.undo[i].is_int)
  2388.     *undobuf.undo[i].where.i = undobuf.undo[i].old_contents.i;
  2389.       else
  2390.     *undobuf.undo[i].where.r = undobuf.undo[i].old_contents.r;
  2391.       
  2392.     }
  2393.  
  2394.   obfree (undobuf.storage);
  2395.   undobuf.num_undo = 0;
  2396.  
  2397.   /* Clear this here, so that subsequent get_last_value calls are not
  2398.      affected.  */
  2399.   subst_prev_insn = NULL_RTX;
  2400. }
  2401.  
  2402. /* Find the innermost point within the rtx at LOC, possibly LOC itself,
  2403.    where we have an arithmetic expression and return that point.  LOC will
  2404.    be inside INSN.
  2405.  
  2406.    try_combine will call this function to see if an insn can be split into
  2407.    two insns.  */
  2408.  
  2409. static rtx *
  2410. find_split_point (loc, insn)
  2411.      rtx *loc;
  2412.      rtx insn;
  2413. {
  2414.   rtx x = *loc;
  2415.   enum rtx_code code = GET_CODE (x);
  2416.   rtx *split;
  2417.   int len = 0, pos, unsignedp;
  2418.   rtx inner;
  2419.  
  2420.   /* First special-case some codes.  */
  2421.   switch (code)
  2422.     {
  2423.     case SUBREG:
  2424. #ifdef INSN_SCHEDULING
  2425.       /* If we are making a paradoxical SUBREG invalid, it becomes a split
  2426.      point.  */
  2427.       if (GET_CODE (SUBREG_REG (x)) == MEM)
  2428.     return loc;
  2429. #endif
  2430.       return find_split_point (&SUBREG_REG (x), insn);
  2431.  
  2432.     case MEM:
  2433. #ifdef HAVE_lo_sum
  2434.       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
  2435.      using LO_SUM and HIGH.  */
  2436.       if (GET_CODE (XEXP (x, 0)) == CONST
  2437.       || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
  2438.     {
  2439.       SUBST (XEXP (x, 0),
  2440.          gen_rtx_combine (LO_SUM, Pmode,
  2441.                   gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
  2442.                   XEXP (x, 0)));
  2443.       return &XEXP (XEXP (x, 0), 0);
  2444.     }
  2445. #endif
  2446.  
  2447.       /* If we have a PLUS whose second operand is a constant and the
  2448.      address is not valid, perhaps will can split it up using
  2449.      the machine-specific way to split large constants.  We use
  2450.      the first pseudo-reg (one of the virtual regs) as a placeholder;
  2451.      it will not remain in the result.  */
  2452.       if (GET_CODE (XEXP (x, 0)) == PLUS
  2453.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  2454.       && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
  2455.     {
  2456.       rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
  2457.       rtx seq = split_insns (gen_rtx (SET, VOIDmode, reg, XEXP (x, 0)),
  2458.                  subst_insn);
  2459.  
  2460.       /* This should have produced two insns, each of which sets our
  2461.          placeholder.  If the source of the second is a valid address,
  2462.          we can make put both sources together and make a split point
  2463.          in the middle.  */
  2464.  
  2465.       if (seq && XVECLEN (seq, 0) == 2
  2466.           && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
  2467.           && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
  2468.           && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
  2469.           && ! reg_mentioned_p (reg,
  2470.                     SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
  2471.           && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
  2472.           && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
  2473.           && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
  2474.           && memory_address_p (GET_MODE (x),
  2475.                    SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
  2476.         {
  2477.           rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
  2478.           rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
  2479.  
  2480.           /* Replace the placeholder in SRC2 with SRC1.  If we can
  2481.          find where in SRC2 it was placed, that can become our
  2482.          split point and we can replace this address with SRC2.
  2483.          Just try two obvious places.  */
  2484.  
  2485.           src2 = replace_rtx (src2, reg, src1);
  2486.           split = 0;
  2487.           if (XEXP (src2, 0) == src1)
  2488.         split = &XEXP (src2, 0);
  2489.           else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
  2490.                && XEXP (XEXP (src2, 0), 0) == src1)
  2491.         split = &XEXP (XEXP (src2, 0), 0);
  2492.  
  2493.           if (split)
  2494.         {
  2495.           SUBST (XEXP (x, 0), src2);
  2496.           return split;
  2497.         }
  2498.         }
  2499.       
  2500.       /* If that didn't work, perhaps the first operand is complex and
  2501.          needs to be computed separately, so make a split point there.
  2502.          This will occur on machines that just support REG + CONST
  2503.          and have a constant moved through some previous computation.  */
  2504.  
  2505.       else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
  2506.            && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
  2507.              && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
  2508.                  == 'o')))
  2509.         return &XEXP (XEXP (x, 0), 0);
  2510.     }
  2511.       break;
  2512.  
  2513.     case SET:
  2514. #ifdef HAVE_cc0
  2515.       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
  2516.      ZERO_EXTRACT, the most likely reason why this doesn't match is that
  2517.      we need to put the operand into a register.  So split at that
  2518.      point.  */
  2519.  
  2520.       if (SET_DEST (x) == cc0_rtx
  2521.       && GET_CODE (SET_SRC (x)) != COMPARE
  2522.       && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
  2523.       && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
  2524.       && ! (GET_CODE (SET_SRC (x)) == SUBREG
  2525.         && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
  2526.     return &SET_SRC (x);
  2527. #endif
  2528.  
  2529.       /* See if we can split SET_SRC as it stands.  */
  2530.       split = find_split_point (&SET_SRC (x), insn);
  2531.       if (split && split != &SET_SRC (x))
  2532.     return split;
  2533.  
  2534.       /* See if this is a bitfield assignment with everything constant.  If
  2535.      so, this is an IOR of an AND, so split it into that.  */
  2536.       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
  2537.       && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
  2538.           <= HOST_BITS_PER_WIDE_INT)
  2539.       && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
  2540.       && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
  2541.       && GET_CODE (SET_SRC (x)) == CONST_INT
  2542.       && ((INTVAL (XEXP (SET_DEST (x), 1))
  2543.           + INTVAL (XEXP (SET_DEST (x), 2)))
  2544.           <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
  2545.       && ! side_effects_p (XEXP (SET_DEST (x), 0)))
  2546.     {
  2547.       int pos = INTVAL (XEXP (SET_DEST (x), 2));
  2548.       int len = INTVAL (XEXP (SET_DEST (x), 1));
  2549.       int src = INTVAL (SET_SRC (x));
  2550.       rtx dest = XEXP (SET_DEST (x), 0);
  2551.       enum machine_mode mode = GET_MODE (dest);
  2552.       unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
  2553.  
  2554.       if (BITS_BIG_ENDIAN)
  2555.         pos = GET_MODE_BITSIZE (mode) - len - pos;
  2556.  
  2557.       if (src == mask)
  2558.         SUBST (SET_SRC (x),
  2559.            gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
  2560.       else
  2561.         SUBST (SET_SRC (x),
  2562.            gen_binary (IOR, mode,
  2563.                    gen_binary (AND, mode, dest, 
  2564.                        GEN_INT (~ (mask << pos)
  2565.                             & GET_MODE_MASK (mode))),
  2566.                    GEN_INT (src << pos)));
  2567.  
  2568.       SUBST (SET_DEST (x), dest);
  2569.  
  2570.       split = find_split_point (&SET_SRC (x), insn);
  2571.       if (split && split != &SET_SRC (x))
  2572.         return split;
  2573.     }
  2574.  
  2575.       /* Otherwise, see if this is an operation that we can split into two.
  2576.      If so, try to split that.  */
  2577.       code = GET_CODE (SET_SRC (x));
  2578.  
  2579.       switch (code)
  2580.     {
  2581.     case AND:
  2582.       /* If we are AND'ing with a large constant that is only a single
  2583.          bit and the result is only being used in a context where we
  2584.          need to know if it is zero or non-zero, replace it with a bit
  2585.          extraction.  This will avoid the large constant, which might
  2586.          have taken more than one insn to make.  If the constant were
  2587.          not a valid argument to the AND but took only one insn to make,
  2588.          this is no worse, but if it took more than one insn, it will
  2589.          be better.  */
  2590.  
  2591.       if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
  2592.           && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
  2593.           && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
  2594.           && GET_CODE (SET_DEST (x)) == REG
  2595.           && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
  2596.           && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
  2597.           && XEXP (*split, 0) == SET_DEST (x)
  2598.           && XEXP (*split, 1) == const0_rtx)
  2599.         {
  2600.           SUBST (SET_SRC (x),
  2601.              make_extraction (GET_MODE (SET_DEST (x)),
  2602.                       XEXP (SET_SRC (x), 0),
  2603.                       pos, NULL_RTX, 1, 1, 0, 0));
  2604.           return find_split_point (loc, insn);
  2605.         }
  2606.       break;
  2607.  
  2608.     case SIGN_EXTEND:
  2609.       inner = XEXP (SET_SRC (x), 0);
  2610.       pos = 0;
  2611.       len = GET_MODE_BITSIZE (GET_MODE (inner));
  2612.       unsignedp = 0;
  2613.       break;
  2614.  
  2615.     case SIGN_EXTRACT:
  2616.     case ZERO_EXTRACT:
  2617.       if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
  2618.           && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
  2619.         {
  2620.           inner = XEXP (SET_SRC (x), 0);
  2621.           len = INTVAL (XEXP (SET_SRC (x), 1));
  2622.           pos = INTVAL (XEXP (SET_SRC (x), 2));
  2623.  
  2624.           if (BITS_BIG_ENDIAN)
  2625.         pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
  2626.           unsignedp = (code == ZERO_EXTRACT);
  2627.         }
  2628.       break;
  2629.     }
  2630.  
  2631.       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
  2632.     {
  2633.       enum machine_mode mode = GET_MODE (SET_SRC (x));
  2634.  
  2635.       /* For unsigned, we have a choice of a shift followed by an
  2636.          AND or two shifts.  Use two shifts for field sizes where the
  2637.          constant might be too large.  We assume here that we can
  2638.          always at least get 8-bit constants in an AND insn, which is
  2639.          true for every current RISC.  */
  2640.  
  2641.       if (unsignedp && len <= 8)
  2642.         {
  2643.           SUBST (SET_SRC (x),
  2644.              gen_rtx_combine
  2645.              (AND, mode,
  2646.               gen_rtx_combine (LSHIFTRT, mode,
  2647.                        gen_lowpart_for_combine (mode, inner),
  2648.                        GEN_INT (pos)),
  2649.               GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
  2650.  
  2651.           split = find_split_point (&SET_SRC (x), insn);
  2652.           if (split && split != &SET_SRC (x))
  2653.         return split;
  2654.         }
  2655.       else
  2656.         {
  2657.           SUBST (SET_SRC (x),
  2658.              gen_rtx_combine
  2659.              (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
  2660.               gen_rtx_combine (ASHIFT, mode,
  2661.                        gen_lowpart_for_combine (mode, inner),
  2662.                        GEN_INT (GET_MODE_BITSIZE (mode)
  2663.                         - len - pos)),
  2664.               GEN_INT (GET_MODE_BITSIZE (mode) - len)));
  2665.  
  2666.           split = find_split_point (&SET_SRC (x), insn);
  2667.           if (split && split != &SET_SRC (x))
  2668.         return split;
  2669.         }
  2670.     }
  2671.  
  2672.       /* See if this is a simple operation with a constant as the second
  2673.      operand.  It might be that this constant is out of range and hence
  2674.      could be used as a split point.  */
  2675.       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
  2676.        || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
  2677.        || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
  2678.       && CONSTANT_P (XEXP (SET_SRC (x), 1))
  2679.       && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
  2680.           || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
  2681.           && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
  2682.               == 'o'))))
  2683.     return &XEXP (SET_SRC (x), 1);
  2684.  
  2685.       /* Finally, see if this is a simple operation with its first operand
  2686.      not in a register.  The operation might require this operand in a
  2687.      register, so return it as a split point.  We can always do this
  2688.      because if the first operand were another operation, we would have
  2689.      already found it as a split point.  */
  2690.       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
  2691.        || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
  2692.        || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
  2693.        || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
  2694.       && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
  2695.     return &XEXP (SET_SRC (x), 0);
  2696.  
  2697.       return 0;
  2698.  
  2699.     case AND:
  2700.     case IOR:
  2701.       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
  2702.      it is better to write this as (not (ior A B)) so we can split it.
  2703.      Similarly for IOR.  */
  2704.       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
  2705.     {
  2706.       SUBST (*loc,
  2707.          gen_rtx_combine (NOT, GET_MODE (x),
  2708.                   gen_rtx_combine (code == IOR ? AND : IOR,
  2709.                            GET_MODE (x),
  2710.                            XEXP (XEXP (x, 0), 0),
  2711.                            XEXP (XEXP (x, 1), 0))));
  2712.       return find_split_point (loc, insn);
  2713.     }
  2714.  
  2715.       /* Many RISC machines have a large set of logical insns.  If the
  2716.      second operand is a NOT, put it first so we will try to split the
  2717.      other operand first.  */
  2718.       if (GET_CODE (XEXP (x, 1)) == NOT)
  2719.     {
  2720.       rtx tem = XEXP (x, 0);
  2721.       SUBST (XEXP (x, 0), XEXP (x, 1));
  2722.       SUBST (XEXP (x, 1), tem);
  2723.     }
  2724.       break;
  2725.     }
  2726.  
  2727.   /* Otherwise, select our actions depending on our rtx class.  */
  2728.   switch (GET_RTX_CLASS (code))
  2729.     {
  2730.     case 'b':            /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
  2731.     case '3':
  2732.       split = find_split_point (&XEXP (x, 2), insn);
  2733.       if (split)
  2734.     return split;
  2735.       /* ... fall through ... */
  2736.     case '2':
  2737.     case 'c':
  2738.     case '<':
  2739.       split = find_split_point (&XEXP (x, 1), insn);
  2740.       if (split)
  2741.     return split;
  2742.       /* ... fall through ... */
  2743.     case '1':
  2744.       /* Some machines have (and (shift ...) ...) insns.  If X is not
  2745.      an AND, but XEXP (X, 0) is, use it as our split point.  */
  2746.       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
  2747.     return &XEXP (x, 0);
  2748.  
  2749.       split = find_split_point (&XEXP (x, 0), insn);
  2750.       if (split)
  2751.     return split;
  2752.       return loc;
  2753.     }
  2754.  
  2755.   /* Otherwise, we don't have a split point.  */
  2756.   return 0;
  2757. }
  2758.  
  2759. /* Throughout X, replace FROM with TO, and return the result.
  2760.    The result is TO if X is FROM;
  2761.    otherwise the result is X, but its contents may have been modified.
  2762.    If they were modified, a record was made in undobuf so that
  2763.    undo_all will (among other things) return X to its original state.
  2764.  
  2765.    If the number of changes necessary is too much to record to undo,
  2766.    the excess changes are not made, so the result is invalid.
  2767.    The changes already made can still be undone.
  2768.    undobuf.num_undo is incremented for such changes, so by testing that
  2769.    the caller can tell whether the result is valid.
  2770.  
  2771.    `n_occurrences' is incremented each time FROM is replaced.
  2772.    
  2773.    IN_DEST is non-zero if we are processing the SET_DEST of a SET.
  2774.  
  2775.    UNIQUE_COPY is non-zero if each substitution must be unique.  We do this
  2776.    by copying if `n_occurrences' is non-zero.  */
  2777.  
  2778. static rtx
  2779. subst (x, from, to, in_dest, unique_copy)
  2780.      register rtx x, from, to;
  2781.      int in_dest;
  2782.      int unique_copy;
  2783. {
  2784.   register enum rtx_code code = GET_CODE (x);
  2785.   enum machine_mode op0_mode = VOIDmode;
  2786.   register char *fmt;
  2787.   register int len, i;
  2788.   rtx new;
  2789.  
  2790. /* Two expressions are equal if they are identical copies of a shared
  2791.    RTX or if they are both registers with the same register number
  2792.    and mode.  */
  2793.  
  2794. #define COMBINE_RTX_EQUAL_P(X,Y)            \
  2795.   ((X) == (Y)                        \
  2796.    || (GET_CODE (X) == REG && GET_CODE (Y) == REG    \
  2797.        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
  2798.  
  2799.   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
  2800.     {
  2801.       n_occurrences++;
  2802.       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
  2803.     }
  2804.  
  2805.   /* If X and FROM are the same register but different modes, they will
  2806.      not have been seen as equal above.  However, flow.c will make a 
  2807.      LOG_LINKS entry for that case.  If we do nothing, we will try to
  2808.      rerecognize our original insn and, when it succeeds, we will
  2809.      delete the feeding insn, which is incorrect.
  2810.  
  2811.      So force this insn not to match in this (rare) case.  */
  2812.   if (! in_dest && code == REG && GET_CODE (from) == REG
  2813.       && REGNO (x) == REGNO (from))
  2814.     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
  2815.  
  2816.   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
  2817.      of which may contain things that can be combined.  */
  2818.   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
  2819.     return x;
  2820.  
  2821.   /* It is possible to have a subexpression appear twice in the insn.
  2822.      Suppose that FROM is a register that appears within TO.
  2823.      Then, after that subexpression has been scanned once by `subst',
  2824.      the second time it is scanned, TO may be found.  If we were
  2825.      to scan TO here, we would find FROM within it and create a
  2826.      self-referent rtl structure which is completely wrong.  */
  2827.   if (COMBINE_RTX_EQUAL_P (x, to))
  2828.     return to;
  2829.  
  2830.   len = GET_RTX_LENGTH (code);
  2831.   fmt = GET_RTX_FORMAT (code);
  2832.  
  2833.   /* We don't need to process a SET_DEST that is a register, CC0, or PC, so
  2834.      set up to skip this common case.  All other cases where we want to
  2835.      suppress replacing something inside a SET_SRC are handled via the
  2836.      IN_DEST operand.  */
  2837.   if (code == SET
  2838.       && (GET_CODE (SET_DEST (x)) == REG
  2839.         || GET_CODE (SET_DEST (x)) == CC0
  2840.         || GET_CODE (SET_DEST (x)) == PC))
  2841.     fmt = "ie";
  2842.  
  2843.   /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a constant. */
  2844.   if (fmt[0] == 'e')
  2845.     op0_mode = GET_MODE (XEXP (x, 0));
  2846.  
  2847.   for (i = 0; i < len; i++)
  2848.     {
  2849.       if (fmt[i] == 'E')
  2850.     {
  2851.       register int j;
  2852.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2853.         {
  2854.           if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
  2855.         {
  2856.           new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
  2857.           n_occurrences++;
  2858.         }
  2859.           else
  2860.         {
  2861.           new = subst (XVECEXP (x, i, j), from, to, 0, unique_copy);
  2862.  
  2863.           /* If this substitution failed, this whole thing fails.  */
  2864.           if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
  2865.             return new;
  2866.         }
  2867.  
  2868.           SUBST (XVECEXP (x, i, j), new);
  2869.         }
  2870.     }
  2871.       else if (fmt[i] == 'e')
  2872.     {
  2873.       if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
  2874.         {
  2875.           /* In general, don't install a subreg involving two modes not
  2876.          tieable.  It can worsen register allocation, and can even
  2877.          make invalid reload insns, since the reg inside may need to
  2878.          be copied from in the outside mode, and that may be invalid
  2879.          if it is an fp reg copied in integer mode.
  2880.  
  2881.          We allow two exceptions to this: It is valid if it is inside
  2882.          another SUBREG and the mode of that SUBREG and the mode of
  2883.          the inside of TO is tieable and it is valid if X is a SET
  2884.          that copies FROM to CC0.  */
  2885.           if (GET_CODE (to) == SUBREG
  2886.           && ! MODES_TIEABLE_P (GET_MODE (to),
  2887.                     GET_MODE (SUBREG_REG (to)))
  2888.           && ! (code == SUBREG
  2889.             && MODES_TIEABLE_P (GET_MODE (x),
  2890.                         GET_MODE (SUBREG_REG (to))))
  2891. #ifdef HAVE_cc0
  2892.           && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
  2893. #endif
  2894.           )
  2895.         return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  2896.  
  2897.           new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
  2898.           n_occurrences++;
  2899.         }
  2900.       else
  2901.         /* If we are in a SET_DEST, suppress most cases unless we
  2902.            have gone inside a MEM, in which case we want to
  2903.            simplify the address.  We assume here that things that
  2904.            are actually part of the destination have their inner
  2905.            parts in the first expression.  This is true for SUBREG, 
  2906.            STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
  2907.            things aside from REG and MEM that should appear in a
  2908.            SET_DEST.  */
  2909.         new = subst (XEXP (x, i), from, to,
  2910.              (((in_dest
  2911.                 && (code == SUBREG || code == STRICT_LOW_PART
  2912.                 || code == ZERO_EXTRACT))
  2913.                || code == SET)
  2914.               && i == 0), unique_copy);
  2915.  
  2916.       /* If we found that we will have to reject this combination,
  2917.          indicate that by returning the CLOBBER ourselves, rather than
  2918.          an expression containing it.  This will speed things up as
  2919.          well as prevent accidents where two CLOBBERs are considered
  2920.          to be equal, thus producing an incorrect simplification.  */
  2921.  
  2922.       if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
  2923.         return new;
  2924.  
  2925.       SUBST (XEXP (x, i), new);
  2926.     }
  2927.     }
  2928.  
  2929.   /* Try to simplify X.  If the simplification changed the code, it is likely
  2930.      that further simplification will help, so loop, but limit the number
  2931.      of repetitions that will be performed.  */
  2932.  
  2933.   for (i = 0; i < 4; i++)
  2934.     {
  2935.       /* If X is sufficiently simple, don't bother trying to do anything
  2936.      with it.  */
  2937.       if (code != CONST_INT && code != REG && code != CLOBBER)
  2938.     x = simplify_rtx (x, op0_mode, i == 3, in_dest);
  2939.  
  2940.       if (GET_CODE (x) == code)
  2941.     break;
  2942.  
  2943.       code = GET_CODE (x);
  2944.  
  2945.       /* We no longer know the original mode of operand 0 since we
  2946.      have changed the form of X)  */
  2947.       op0_mode = VOIDmode;
  2948.     }
  2949.  
  2950.   return x;
  2951. }
  2952.  
  2953. /* Simplify X, a piece of RTL.  We just operate on the expression at the
  2954.    outer level; call `subst' to simplify recursively.  Return the new
  2955.    expression.
  2956.  
  2957.    OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
  2958.    will be the iteration even if an expression with a code different from
  2959.    X is returned; IN_DEST is nonzero if we are inside a SET_DEST.  */
  2960.  
  2961. static rtx
  2962. simplify_rtx (x, op0_mode, last, in_dest)
  2963.      rtx x;
  2964.      enum machine_mode op0_mode;
  2965.      int last;
  2966.      int in_dest;
  2967. {
  2968.   enum rtx_code code = GET_CODE (x);
  2969.   enum machine_mode mode = GET_MODE (x);
  2970.   rtx temp;
  2971.   int i;
  2972.  
  2973.   /* If this is a commutative operation, put a constant last and a complex
  2974.      expression first.  We don't need to do this for comparisons here.  */
  2975.   if (GET_RTX_CLASS (code) == 'c'
  2976.       && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
  2977.       || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
  2978.           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
  2979.       || (GET_CODE (XEXP (x, 0)) == SUBREG
  2980.           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
  2981.           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
  2982.     {
  2983.       temp = XEXP (x, 0);
  2984.       SUBST (XEXP (x, 0), XEXP (x, 1));
  2985.       SUBST (XEXP (x, 1), temp);
  2986.     }
  2987.  
  2988.   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
  2989.      sign extension of a PLUS with a constant, reverse the order of the sign
  2990.      extension and the addition. Note that this not the same as the original
  2991.      code, but overflow is undefined for signed values.  Also note that the
  2992.      PLUS will have been partially moved "inside" the sign-extension, so that
  2993.      the first operand of X will really look like:
  2994.          (ashiftrt (plus (ashift A C4) C5) C4).
  2995.      We convert this to
  2996.          (plus (ashiftrt (ashift A C4) C2) C4)
  2997.      and replace the first operand of X with that expression.  Later parts
  2998.      of this function may simplify the expression further.
  2999.  
  3000.      For example, if we start with (mult (sign_extend (plus A C1)) C2),
  3001.      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
  3002.      distributive law to produce (plus (mult (sign_extend X) C1) C3).
  3003.  
  3004.      We do this to simplify address expressions.  */
  3005.  
  3006.   if ((code == PLUS || code == MINUS || code == MULT)
  3007.       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
  3008.       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
  3009.       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
  3010.       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
  3011.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  3012.       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
  3013.       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
  3014.       && (temp = simplify_binary_operation (ASHIFTRT, mode,
  3015.                         XEXP (XEXP (XEXP (x, 0), 0), 1),
  3016.                         XEXP (XEXP (x, 0), 1))) != 0)
  3017.     {
  3018.       rtx new
  3019.     = simplify_shift_const (NULL_RTX, ASHIFT, mode,
  3020.                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
  3021.                 INTVAL (XEXP (XEXP (x, 0), 1)));
  3022.  
  3023.       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
  3024.                   INTVAL (XEXP (XEXP (x, 0), 1)));
  3025.  
  3026.       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
  3027.     }
  3028.  
  3029.   /* If this is a simple operation applied to an IF_THEN_ELSE, try 
  3030.      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
  3031.      things.  Check for cases where both arms are testing the same
  3032.      condition.
  3033.  
  3034.      Don't do anything if all operands are very simple.  */
  3035.  
  3036.   if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
  3037.     || GET_RTX_CLASS (code) == '<')
  3038.        && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
  3039.         && ! (GET_CODE (XEXP (x, 0)) == SUBREG
  3040.           && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
  3041.               == 'o')))
  3042.        || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
  3043.            && ! (GET_CODE (XEXP (x, 1)) == SUBREG
  3044.              && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
  3045.              == 'o')))))
  3046.       || (GET_RTX_CLASS (code) == '1'
  3047.       && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
  3048.            && ! (GET_CODE (XEXP (x, 0)) == SUBREG
  3049.              && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
  3050.              == 'o'))))))
  3051.     {
  3052.       rtx cond, true, false;
  3053.  
  3054.       cond = if_then_else_cond (x, &true, &false);
  3055.       if (cond != 0)
  3056.     {
  3057.       rtx cop1 = const0_rtx;
  3058.       enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
  3059.  
  3060.       /* Simplify the alternative arms; this may collapse the true and 
  3061.          false arms to store-flag values.  */
  3062.       true = subst (true, pc_rtx, pc_rtx, 0, 0);
  3063.       false = subst (false, pc_rtx, pc_rtx, 0, 0);
  3064.  
  3065.       /* Restarting if we generate a store-flag expression will cause
  3066.          us to loop.  Just drop through in this case.  */
  3067.  
  3068.       /* If the result values are STORE_FLAG_VALUE and zero, we can
  3069.          just make the comparison operation.  */
  3070.       if (true == const_true_rtx && false == const0_rtx)
  3071.         x = gen_binary (cond_code, mode, cond, cop1);
  3072.       else if (true == const0_rtx && false == const_true_rtx)
  3073.         x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
  3074.  
  3075.       /* Likewise, we can make the negate of a comparison operation
  3076.          if the result values are - STORE_FLAG_VALUE and zero.  */
  3077.       else if (GET_CODE (true) == CONST_INT
  3078.            && INTVAL (true) == - STORE_FLAG_VALUE
  3079.            && false == const0_rtx)
  3080.         x = gen_unary (NEG, mode, mode,
  3081.                gen_binary (cond_code, mode, cond, cop1));
  3082.       else if (GET_CODE (false) == CONST_INT
  3083.            && INTVAL (false) == - STORE_FLAG_VALUE
  3084.            && true == const0_rtx)
  3085.         x = gen_unary (NEG, mode, mode,
  3086.                gen_binary (reverse_condition (cond_code), 
  3087.                        mode, cond, cop1));
  3088.       else
  3089.         return gen_rtx (IF_THEN_ELSE, mode,
  3090.                 gen_binary (cond_code, VOIDmode, cond, cop1),
  3091.                 true, false);
  3092.  
  3093.       code = GET_CODE (x);
  3094.       op0_mode = VOIDmode;
  3095.     }
  3096.     }
  3097.  
  3098.   /* Try to fold this expression in case we have constants that weren't
  3099.      present before.  */
  3100.   temp = 0;
  3101.   switch (GET_RTX_CLASS (code))
  3102.     {
  3103.     case '1':
  3104.       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
  3105.       break;
  3106.     case '<':
  3107.       temp = simplify_relational_operation (code, op0_mode,
  3108.                         XEXP (x, 0), XEXP (x, 1));
  3109. #ifdef FLOAT_STORE_FLAG_VALUE
  3110.       if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
  3111.     temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
  3112.         : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
  3113. #endif
  3114.       break;
  3115.     case 'c':
  3116.     case '2':
  3117.       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
  3118.       break;
  3119.     case 'b':
  3120.     case '3':
  3121.       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
  3122.                      XEXP (x, 1), XEXP (x, 2));
  3123.       break;
  3124.     }
  3125.  
  3126.   if (temp)
  3127.     x = temp, code = GET_CODE (temp);
  3128.  
  3129.   /* First see if we can apply the inverse distributive law.  */
  3130.   if (code == PLUS || code == MINUS
  3131.       || code == AND || code == IOR || code == XOR)
  3132.     {
  3133.       x = apply_distributive_law (x);
  3134.       code = GET_CODE (x);
  3135.     }
  3136.  
  3137.   /* If CODE is an associative operation not otherwise handled, see if we
  3138.      can associate some operands.  This can win if they are constants or
  3139.      if they are logically related (i.e. (a & b) & a.  */
  3140.   if ((code == PLUS || code == MINUS
  3141.        || code == MULT || code == AND || code == IOR || code == XOR
  3142.        || code == DIV || code == UDIV
  3143.        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
  3144.       && INTEGRAL_MODE_P (mode))
  3145.     {
  3146.       if (GET_CODE (XEXP (x, 0)) == code)
  3147.     {
  3148.       rtx other = XEXP (XEXP (x, 0), 0);
  3149.       rtx inner_op0 = XEXP (XEXP (x, 0), 1);
  3150.       rtx inner_op1 = XEXP (x, 1);
  3151.       rtx inner;
  3152.       
  3153.       /* Make sure we pass the constant operand if any as the second
  3154.          one if this is a commutative operation.  */
  3155.       if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
  3156.         {
  3157.           rtx tem = inner_op0;
  3158.           inner_op0 = inner_op1;
  3159.           inner_op1 = tem;
  3160.         }
  3161.       inner = simplify_binary_operation (code == MINUS ? PLUS
  3162.                          : code == DIV ? MULT
  3163.                          : code == UDIV ? MULT
  3164.                          : code,
  3165.                          mode, inner_op0, inner_op1);
  3166.  
  3167.       /* For commutative operations, try the other pair if that one
  3168.          didn't simplify.  */
  3169.       if (inner == 0 && GET_RTX_CLASS (code) == 'c')
  3170.         {
  3171.           other = XEXP (XEXP (x, 0), 1);
  3172.           inner = simplify_binary_operation (code, mode,
  3173.                          XEXP (XEXP (x, 0), 0),
  3174.                          XEXP (x, 1));
  3175.         }
  3176.  
  3177.       if (inner)
  3178.         return gen_binary (code, mode, other, inner);
  3179.     }
  3180.     }
  3181.  
  3182.   /* A little bit of algebraic simplification here.  */
  3183.   switch (code)
  3184.     {
  3185.     case MEM:
  3186.       /* Ensure that our address has any ASHIFTs converted to MULT in case
  3187.      address-recognizing predicates are called later.  */
  3188.       temp = make_compound_operation (XEXP (x, 0), MEM);
  3189.       SUBST (XEXP (x, 0), temp);
  3190.       break;
  3191.  
  3192.     case SUBREG:
  3193.       /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
  3194.      is paradoxical.  If we can't do that safely, then it becomes
  3195.      something nonsensical so that this combination won't take place.  */
  3196.  
  3197.       if (GET_CODE (SUBREG_REG (x)) == MEM
  3198.       && (GET_MODE_SIZE (mode)
  3199.           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
  3200.     {
  3201.       rtx inner = SUBREG_REG (x);
  3202.       int endian_offset = 0;
  3203.       /* Don't change the mode of the MEM
  3204.          if that would change the meaning of the address.  */
  3205.       if (MEM_VOLATILE_P (SUBREG_REG (x))
  3206.           || mode_dependent_address_p (XEXP (inner, 0)))
  3207.         return gen_rtx (CLOBBER, mode, const0_rtx);
  3208.  
  3209.       if (BYTES_BIG_ENDIAN)
  3210.         {
  3211.           if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
  3212.         endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
  3213.           if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
  3214.         endian_offset -= (UNITS_PER_WORD
  3215.                   - GET_MODE_SIZE (GET_MODE (inner)));
  3216.         }
  3217.       /* Note if the plus_constant doesn't make a valid address
  3218.          then this combination won't be accepted.  */
  3219.       x = gen_rtx (MEM, mode,
  3220.                plus_constant (XEXP (inner, 0),
  3221.                       (SUBREG_WORD (x) * UNITS_PER_WORD
  3222.                        + endian_offset)));
  3223.       MEM_VOLATILE_P (x) = MEM_VOLATILE_P (inner);
  3224.       RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
  3225.       MEM_IN_STRUCT_P (x) = MEM_IN_STRUCT_P (inner);
  3226.       return x;
  3227.     }
  3228.  
  3229.       /* If we are in a SET_DEST, these other cases can't apply.  */
  3230.       if (in_dest)
  3231.     return x;
  3232.  
  3233.       /* Changing mode twice with SUBREG => just change it once,
  3234.      or not at all if changing back to starting mode.  */
  3235.       if (GET_CODE (SUBREG_REG (x)) == SUBREG)
  3236.     {
  3237.       if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
  3238.           && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
  3239.         return SUBREG_REG (SUBREG_REG (x));
  3240.  
  3241.       SUBST_INT (SUBREG_WORD (x),
  3242.              SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
  3243.       SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
  3244.     }
  3245.  
  3246.       /* SUBREG of a hard register => just change the register number
  3247.      and/or mode.  If the hard register is not valid in that mode,
  3248.      suppress this combination.  If the hard register is the stack,
  3249.      frame, or argument pointer, leave this as a SUBREG.  */
  3250.  
  3251.       if (GET_CODE (SUBREG_REG (x)) == REG
  3252.       && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
  3253.       && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
  3254. #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
  3255.       && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
  3256. #endif
  3257. #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
  3258.       && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
  3259. #endif
  3260.       && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
  3261.     {
  3262.       if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
  3263.                   mode))
  3264.         return gen_rtx (REG, mode,
  3265.                 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
  3266.       else
  3267.         return gen_rtx (CLOBBER, mode, const0_rtx);
  3268.     }
  3269.  
  3270.       /* For a constant, try to pick up the part we want.  Handle a full
  3271.      word and low-order part.  Only do this if we are narrowing
  3272.      the constant; if it is being widened, we have no idea what
  3273.      the extra bits will have been set to.  */
  3274.  
  3275.       if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
  3276.       && GET_MODE_SIZE (mode) == UNITS_PER_WORD
  3277.       && GET_MODE_SIZE (op0_mode) < UNITS_PER_WORD
  3278.       && GET_MODE_CLASS (mode) == MODE_INT)
  3279.     {
  3280.       temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
  3281.                   0, op0_mode);
  3282.       if (temp)
  3283.         return temp;
  3284.     }
  3285.     
  3286.       /* If we want a subreg of a constant, at offset 0,
  3287.      take the low bits.  On a little-endian machine, that's
  3288.      always valid.  On a big-endian machine, it's valid
  3289.      only if the constant's mode fits in one word.  */
  3290.       if (CONSTANT_P (SUBREG_REG (x)) && subreg_lowpart_p (x)
  3291.       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (op0_mode)
  3292.       && (! WORDS_BIG_ENDIAN
  3293.           || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
  3294.     return gen_lowpart_for_combine (mode, SUBREG_REG (x));
  3295.  
  3296.       /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
  3297.      since we are saying that the high bits don't matter.  */
  3298.       if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
  3299.       && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
  3300.     return SUBREG_REG (x);
  3301.  
  3302.       /* Note that we cannot do any narrowing for non-constants since
  3303.      we might have been counting on using the fact that some bits were
  3304.      zero.  We now do this in the SET.  */
  3305.  
  3306.       break;
  3307.  
  3308.     case NOT:
  3309.       /* (not (plus X -1)) can become (neg X).  */
  3310.       if (GET_CODE (XEXP (x, 0)) == PLUS
  3311.       && XEXP (XEXP (x, 0), 1) == constm1_rtx)
  3312.     return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
  3313.  
  3314.       /* Similarly, (not (neg X)) is (plus X -1).  */
  3315.       if (GET_CODE (XEXP (x, 0)) == NEG)
  3316.     return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
  3317.                 constm1_rtx);
  3318.  
  3319.       /* (not (xor X C)) for C constant is (xor X D) with D = ~ C.  */
  3320.       if (GET_CODE (XEXP (x, 0)) == XOR
  3321.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  3322.       && (temp = simplify_unary_operation (NOT, mode,
  3323.                            XEXP (XEXP (x, 0), 1),
  3324.                            mode)) != 0)
  3325.     return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
  3326.           
  3327.       /* (not (ashift 1 X)) is (rotate ~1 X).  We used to do this for operands
  3328.      other than 1, but that is not valid.  We could do a similar
  3329.      simplification for (not (lshiftrt C X)) where C is just the sign bit,
  3330.      but this doesn't seem common enough to bother with.  */
  3331.       if (GET_CODE (XEXP (x, 0)) == ASHIFT
  3332.       && XEXP (XEXP (x, 0), 0) == const1_rtx)
  3333.     return gen_rtx (ROTATE, mode, gen_unary (NOT, mode, mode, const1_rtx),
  3334.             XEXP (XEXP (x, 0), 1));
  3335.                         
  3336.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  3337.       && subreg_lowpart_p (XEXP (x, 0))
  3338.       && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
  3339.           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
  3340.       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
  3341.       && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
  3342.     {
  3343.       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
  3344.  
  3345.       x = gen_rtx (ROTATE, inner_mode,
  3346.                gen_unary (NOT, inner_mode, inner_mode, const1_rtx),
  3347.                XEXP (SUBREG_REG (XEXP (x, 0)), 1));
  3348.       return gen_lowpart_for_combine (mode, x);
  3349.     }
  3350.                         
  3351. #if STORE_FLAG_VALUE == -1
  3352.       /* (not (comparison foo bar)) can be done by reversing the comparison
  3353.      code if valid.  */
  3354.       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
  3355.       && reversible_comparison_p (XEXP (x, 0)))
  3356.     return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
  3357.                 mode, XEXP (XEXP (x, 0), 0),
  3358.                 XEXP (XEXP (x, 0), 1));
  3359.  
  3360.       /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
  3361.      is (lt foo (const_int 0)), so we can perform the above
  3362.      simplification.  */
  3363.  
  3364.       if (XEXP (x, 1) == const1_rtx
  3365.       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
  3366.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  3367.       && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
  3368.     return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
  3369. #endif
  3370.  
  3371.       /* Apply De Morgan's laws to reduce number of patterns for machines
  3372.       with negating logical insns (and-not, nand, etc.).  If result has
  3373.       only one NOT, put it first, since that is how the patterns are
  3374.       coded.  */
  3375.  
  3376.       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
  3377.      {
  3378.       rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
  3379.  
  3380.      if (GET_CODE (in1) == NOT)
  3381.        in1 = XEXP (in1, 0);
  3382.       else
  3383.        in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
  3384.  
  3385.      if (GET_CODE (in2) == NOT)
  3386.        in2 = XEXP (in2, 0);
  3387.       else if (GET_CODE (in2) == CONST_INT
  3388.           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
  3389.        in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
  3390.      else
  3391.        in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
  3392.  
  3393.      if (GET_CODE (in2) == NOT)
  3394.        {
  3395.          rtx tem = in2;
  3396.          in2 = in1; in1 = tem;
  3397.        }
  3398.  
  3399.      return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
  3400.                  mode, in1, in2);
  3401.        } 
  3402.       break;
  3403.  
  3404.     case NEG:
  3405.       /* (neg (plus X 1)) can become (not X).  */
  3406.       if (GET_CODE (XEXP (x, 0)) == PLUS
  3407.       && XEXP (XEXP (x, 0), 1) == const1_rtx)
  3408.     return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
  3409.  
  3410.       /* Similarly, (neg (not X)) is (plus X 1).  */
  3411.       if (GET_CODE (XEXP (x, 0)) == NOT)
  3412.     return plus_constant (XEXP (XEXP (x, 0), 0), 1);
  3413.  
  3414.       /* (neg (minus X Y)) can become (minus Y X).  */
  3415.       if (GET_CODE (XEXP (x, 0)) == MINUS
  3416.       && (! FLOAT_MODE_P (mode)
  3417.           /* x-y != -(y-x) with IEEE floating point. */
  3418.           || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3419.           || flag_fast_math))
  3420.     return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
  3421.                XEXP (XEXP (x, 0), 0));
  3422.  
  3423.       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
  3424.       if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
  3425.       && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
  3426.     return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
  3427.  
  3428.       /* NEG commutes with ASHIFT since it is multiplication.  Only do this
  3429.      if we can then eliminate the NEG (e.g.,
  3430.      if the operand is a constant).  */
  3431.  
  3432.       if (GET_CODE (XEXP (x, 0)) == ASHIFT)
  3433.     {
  3434.       temp = simplify_unary_operation (NEG, mode,
  3435.                        XEXP (XEXP (x, 0), 0), mode);
  3436.       if (temp)
  3437.         {
  3438.           SUBST (XEXP (XEXP (x, 0), 0), temp);
  3439.           return XEXP (x, 0);
  3440.         }
  3441.     }
  3442.  
  3443.       temp = expand_compound_operation (XEXP (x, 0));
  3444.  
  3445.       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
  3446.       replaced by (lshiftrt X C).  This will convert
  3447.      (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
  3448.  
  3449.       if (GET_CODE (temp) == ASHIFTRT
  3450.       && GET_CODE (XEXP (temp, 1)) == CONST_INT
  3451.       && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
  3452.     return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
  3453.                      INTVAL (XEXP (temp, 1)));
  3454.  
  3455.       /* If X has only a single bit that might be nonzero, say, bit I, convert
  3456.      (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
  3457.      MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
  3458.      (sign_extract X 1 Y).  But only do this if TEMP isn't a register
  3459.      or a SUBREG of one since we'd be making the expression more
  3460.      complex if it was just a register.  */
  3461.  
  3462.       if (GET_CODE (temp) != REG
  3463.       && ! (GET_CODE (temp) == SUBREG
  3464.         && GET_CODE (SUBREG_REG (temp)) == REG)
  3465.       && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
  3466.     {
  3467.       rtx temp1 = simplify_shift_const
  3468.         (NULL_RTX, ASHIFTRT, mode,
  3469.          simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
  3470.                    GET_MODE_BITSIZE (mode) - 1 - i),
  3471.          GET_MODE_BITSIZE (mode) - 1 - i);
  3472.  
  3473.       /* If all we did was surround TEMP with the two shifts, we
  3474.          haven't improved anything, so don't use it.  Otherwise,
  3475.          we are better off with TEMP1.  */
  3476.       if (GET_CODE (temp1) != ASHIFTRT
  3477.           || GET_CODE (XEXP (temp1, 0)) != ASHIFT
  3478.           || XEXP (XEXP (temp1, 0), 0) != temp)
  3479.         return temp1;
  3480.     }
  3481.       break;
  3482.  
  3483.     case TRUNCATE:
  3484.       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
  3485.     SUBST (XEXP (x, 0),
  3486.            force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
  3487.                   GET_MODE_MASK (mode), NULL_RTX, 0));
  3488.       break;
  3489.  
  3490.     case FLOAT_TRUNCATE:
  3491.       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
  3492.       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
  3493.       && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
  3494.      return XEXP (XEXP (x, 0), 0);
  3495.  
  3496.       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
  3497.      (OP:SF foo:SF) if OP is NEG or ABS.  */
  3498.       if ((GET_CODE (XEXP (x, 0)) == ABS
  3499.        || GET_CODE (XEXP (x, 0)) == NEG)
  3500.       && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
  3501.       && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
  3502.     return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
  3503.               XEXP (XEXP (XEXP (x, 0), 0), 0));
  3504.  
  3505.       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
  3506.      is (float_truncate:SF x).  */
  3507.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  3508.       && subreg_lowpart_p (XEXP (x, 0))
  3509.       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
  3510.     return SUBREG_REG (XEXP (x, 0));
  3511.       break;  
  3512.  
  3513. #ifdef HAVE_cc0
  3514.     case COMPARE:
  3515.       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
  3516.      using cc0, in which case we want to leave it as a COMPARE
  3517.      so we can distinguish it from a register-register-copy.  */
  3518.       if (XEXP (x, 1) == const0_rtx)
  3519.     return XEXP (x, 0);
  3520.  
  3521.       /* In IEEE floating point, x-0 is not the same as x.  */
  3522.       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3523.        || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
  3524.        || flag_fast_math)
  3525.       && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
  3526.     return XEXP (x, 0);
  3527.       break;
  3528. #endif
  3529.  
  3530.     case CONST:
  3531.       /* (const (const X)) can become (const X).  Do it this way rather than
  3532.      returning the inner CONST since CONST can be shared with a
  3533.      REG_EQUAL note.  */
  3534.       if (GET_CODE (XEXP (x, 0)) == CONST)
  3535.     SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  3536.       break;
  3537.  
  3538. #ifdef HAVE_lo_sum
  3539.     case LO_SUM:
  3540.       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
  3541.      can add in an offset.  find_split_point will split this address up
  3542.      again if it doesn't match.  */
  3543.       if (GET_CODE (XEXP (x, 0)) == HIGH
  3544.       && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
  3545.     return XEXP (x, 1);
  3546.       break;
  3547. #endif
  3548.  
  3549.     case PLUS:
  3550.       /* If we have (plus (plus (A const) B)), associate it so that CONST is
  3551.      outermost.  That's because that's the way indexed addresses are
  3552.      supposed to appear.  This code used to check many more cases, but
  3553.      they are now checked elsewhere.  */
  3554.       if (GET_CODE (XEXP (x, 0)) == PLUS
  3555.       && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
  3556.     return gen_binary (PLUS, mode,
  3557.                gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
  3558.                        XEXP (x, 1)),
  3559.                XEXP (XEXP (x, 0), 1));
  3560.  
  3561.       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
  3562.      when c is (const_int (pow2 + 1) / 2) is a sign extension of a
  3563.      bit-field and can be replaced by either a sign_extend or a
  3564.      sign_extract.  The `and' may be a zero_extend.  */
  3565.       if (GET_CODE (XEXP (x, 0)) == XOR
  3566.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  3567.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  3568.       && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
  3569.       && (i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
  3570.       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  3571.       && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
  3572.            && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
  3573.            && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
  3574.            == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
  3575.           || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
  3576.           && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
  3577.               == i + 1))))
  3578.     return simplify_shift_const
  3579.       (NULL_RTX, ASHIFTRT, mode,
  3580.        simplify_shift_const (NULL_RTX, ASHIFT, mode,
  3581.                  XEXP (XEXP (XEXP (x, 0), 0), 0),
  3582.                  GET_MODE_BITSIZE (mode) - (i + 1)),
  3583.        GET_MODE_BITSIZE (mode) - (i + 1));
  3584.  
  3585.       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
  3586.      C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
  3587.      is 1.  This produces better code than the alternative immediately
  3588.      below.  */
  3589.       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
  3590.       && reversible_comparison_p (XEXP (x, 0))
  3591.       && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
  3592.           || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
  3593.     return
  3594.       gen_unary (NEG, mode, mode,
  3595.              gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
  3596.                  mode, XEXP (XEXP (x, 0), 0),
  3597.                  XEXP (XEXP (x, 0), 1)));
  3598.  
  3599.       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
  3600.      can become (ashiftrt (ashift (xor x 1) C) C) where C is
  3601.      the bitsize of the mode - 1.  This allows simplification of
  3602.      "a = (b & 8) == 0;"  */
  3603.       if (XEXP (x, 1) == constm1_rtx
  3604.       && GET_CODE (XEXP (x, 0)) != REG
  3605.       && ! (GET_CODE (XEXP (x,0)) == SUBREG
  3606.         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
  3607.       && nonzero_bits (XEXP (x, 0), mode) == 1)
  3608.     return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
  3609.        simplify_shift_const (NULL_RTX, ASHIFT, mode,
  3610.                  gen_rtx_combine (XOR, mode,
  3611.                           XEXP (x, 0), const1_rtx),
  3612.                  GET_MODE_BITSIZE (mode) - 1),
  3613.        GET_MODE_BITSIZE (mode) - 1);
  3614.  
  3615.       /* If we are adding two things that have no bits in common, convert
  3616.      the addition into an IOR.  This will often be further simplified,
  3617.      for example in cases like ((a & 1) + (a & 2)), which can
  3618.      become a & 3.  */
  3619.  
  3620.       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  3621.       && (nonzero_bits (XEXP (x, 0), mode)
  3622.           & nonzero_bits (XEXP (x, 1), mode)) == 0)
  3623.     return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
  3624.       break;
  3625.  
  3626.     case MINUS:
  3627. #if STORE_FLAG_VALUE == 1
  3628.       /* (minus 1 (comparison foo bar)) can be done by reversing the comparison
  3629.      code if valid.  */
  3630.       if (XEXP (x, 0) == const1_rtx
  3631.       && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
  3632.       && reversible_comparison_p (XEXP (x, 1)))
  3633.     return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
  3634.                mode, XEXP (XEXP (x, 1), 0),
  3635.                 XEXP (XEXP (x, 1), 1));
  3636. #endif
  3637.  
  3638.       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
  3639.      (and <foo> (const_int pow2-1))  */
  3640.       if (GET_CODE (XEXP (x, 1)) == AND
  3641.       && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
  3642.       && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
  3643.       && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
  3644.     return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
  3645.                        - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
  3646.  
  3647.       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
  3648.      integers.  */
  3649.       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
  3650.     return gen_binary (MINUS, mode,
  3651.                gen_binary (MINUS, mode, XEXP (x, 0),
  3652.                        XEXP (XEXP (x, 1), 0)),
  3653.                XEXP (XEXP (x, 1), 1));
  3654.       break;
  3655.  
  3656.     case MULT:
  3657.       /* If we have (mult (plus A B) C), apply the distributive law and then
  3658.      the inverse distributive law to see if things simplify.  This
  3659.      occurs mostly in addresses, often when unrolling loops.  */
  3660.  
  3661.       if (GET_CODE (XEXP (x, 0)) == PLUS)
  3662.     {
  3663.       x = apply_distributive_law
  3664.         (gen_binary (PLUS, mode,
  3665.              gen_binary (MULT, mode,
  3666.                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
  3667.              gen_binary (MULT, mode,
  3668.                      XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
  3669.  
  3670.       if (GET_CODE (x) != MULT)
  3671.         return x;
  3672.     }
  3673.       break;
  3674.  
  3675.     case UDIV:
  3676.       /* If this is a divide by a power of two, treat it as a shift if
  3677.      its first operand is a shift.  */
  3678.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  3679.       && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
  3680.       && (GET_CODE (XEXP (x, 0)) == ASHIFT
  3681.           || GET_CODE (XEXP (x, 0)) == LSHIFTRT
  3682.           || GET_CODE (XEXP (x, 0)) == ASHIFTRT
  3683.           || GET_CODE (XEXP (x, 0)) == ROTATE
  3684.           || GET_CODE (XEXP (x, 0)) == ROTATERT))
  3685.     return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
  3686.       break;
  3687.  
  3688.     case EQ:  case NE:
  3689.     case GT:  case GTU:  case GE:  case GEU:
  3690.     case LT:  case LTU:  case LE:  case LEU:
  3691.       /* If the first operand is a condition code, we can't do anything
  3692.      with it.  */
  3693.       if (GET_CODE (XEXP (x, 0)) == COMPARE
  3694.       || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
  3695. #ifdef HAVE_cc0
  3696.           && XEXP (x, 0) != cc0_rtx
  3697. #endif
  3698.            ))
  3699.     {
  3700.       rtx op0 = XEXP (x, 0);
  3701.       rtx op1 = XEXP (x, 1);
  3702.       enum rtx_code new_code;
  3703.  
  3704.       if (GET_CODE (op0) == COMPARE)
  3705.         op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
  3706.  
  3707.       /* Simplify our comparison, if possible.  */
  3708.       new_code = simplify_comparison (code, &op0, &op1);
  3709.  
  3710. #if STORE_FLAG_VALUE == 1
  3711.       /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
  3712.          if only the low-order bit is possibly nonzero in X (such as when
  3713.          X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
  3714.          (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
  3715.          known to be either 0 or -1, NE becomes a NEG and EQ becomes
  3716.          (plus X 1).
  3717.  
  3718.          Remove any ZERO_EXTRACT we made when thinking this was a
  3719.          comparison.  It may now be simpler to use, e.g., an AND.  If a
  3720.          ZERO_EXTRACT is indeed appropriate, it will be placed back by
  3721.          the call to make_compound_operation in the SET case.  */
  3722.  
  3723.       if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  3724.           && op1 == const0_rtx
  3725.           && nonzero_bits (op0, mode) == 1)
  3726.         return gen_lowpart_for_combine (mode,
  3727.                         expand_compound_operation (op0));
  3728.  
  3729.       else if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  3730.            && op1 == const0_rtx
  3731.            && (num_sign_bit_copies (op0, mode)
  3732.                == GET_MODE_BITSIZE (mode)))
  3733.         {
  3734.           op0 = expand_compound_operation (op0);
  3735.           return gen_unary (NEG, mode, mode,
  3736.                 gen_lowpart_for_combine (mode, op0));
  3737.         }
  3738.  
  3739.       else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
  3740.            && op1 == const0_rtx
  3741.            && nonzero_bits (op0, mode) == 1)
  3742.         {
  3743.           op0 = expand_compound_operation (op0);
  3744.           return gen_binary (XOR, mode,
  3745.                  gen_lowpart_for_combine (mode, op0),
  3746.                  const1_rtx);
  3747.         }
  3748.  
  3749.       else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
  3750.            && op1 == const0_rtx
  3751.            && (num_sign_bit_copies (op0, mode)
  3752.                == GET_MODE_BITSIZE (mode)))
  3753.         {
  3754.           op0 = expand_compound_operation (op0);
  3755.           return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
  3756.         }
  3757. #endif
  3758.  
  3759. #if STORE_FLAG_VALUE == -1
  3760.       /* If STORE_FLAG_VALUE is -1, we have cases similar to
  3761.          those above.  */
  3762.       if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  3763.           && op1 == const0_rtx
  3764.           && (num_sign_bit_copies (op0, mode)
  3765.           == GET_MODE_BITSIZE (mode)))
  3766.         return gen_lowpart_for_combine (mode,
  3767.                         expand_compound_operation (op0));
  3768.  
  3769.       else if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  3770.            && op1 == const0_rtx
  3771.            && nonzero_bits (op0, mode) == 1)
  3772.         {
  3773.           op0 = expand_compound_operation (op0);
  3774.           return gen_unary (NEG, mode, mode,
  3775.                 gen_lowpart_for_combine (mode, op0));
  3776.         }
  3777.  
  3778.       else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
  3779.            && op1 == const0_rtx
  3780.            && (num_sign_bit_copies (op0, mode)
  3781.                == GET_MODE_BITSIZE (mode)))
  3782.         {
  3783.           op0 = expand_compound_operation (op0);
  3784.           return gen_unary (NOT, mode, mode,
  3785.                 gen_lowpart_for_combine (mode, op0));
  3786.         }
  3787.  
  3788.       /* If X is 0/1, (eq X 0) is X-1.  */
  3789.       else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
  3790.            && op1 == const0_rtx
  3791.            && nonzero_bits (op0, mode) == 1)
  3792.         {
  3793.           op0 = expand_compound_operation (op0);
  3794.           return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
  3795.         }
  3796. #endif
  3797.  
  3798.       /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
  3799.          one bit that might be nonzero, we can convert (ne x 0) to
  3800.          (ashift x c) where C puts the bit in the sign bit.  Remove any
  3801.          AND with STORE_FLAG_VALUE when we are done, since we are only
  3802.          going to test the sign bit.  */
  3803.       if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  3804.           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  3805.           && (STORE_FLAG_VALUE
  3806.           == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
  3807.           && op1 == const0_rtx
  3808.           && mode == GET_MODE (op0)
  3809.           && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
  3810.         {
  3811.           x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
  3812.                     expand_compound_operation (op0),
  3813.                     GET_MODE_BITSIZE (mode) - 1 - i);
  3814.           if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
  3815.         return XEXP (x, 0);
  3816.           else
  3817.         return x;
  3818.         }
  3819.  
  3820.       /* If the code changed, return a whole new comparison.  */
  3821.       if (new_code != code)
  3822.         return gen_rtx_combine (new_code, mode, op0, op1);
  3823.  
  3824.       /* Otherwise, keep this operation, but maybe change its operands.  
  3825.          This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
  3826.       SUBST (XEXP (x, 0), op0);
  3827.       SUBST (XEXP (x, 1), op1);
  3828.     }
  3829.       break;
  3830.       
  3831.     case IF_THEN_ELSE:
  3832.       return simplify_if_then_else (x);
  3833.  
  3834.     case ZERO_EXTRACT:
  3835.     case SIGN_EXTRACT:
  3836.     case ZERO_EXTEND:
  3837.     case SIGN_EXTEND:
  3838.       /* If we are processing SET_DEST, we are done. */
  3839.       if (in_dest)
  3840.     return x;
  3841.  
  3842.       return expand_compound_operation (x);
  3843.  
  3844.     case SET:
  3845.       return simplify_set (x);
  3846.  
  3847.     case AND:
  3848.     case IOR:
  3849.     case XOR:
  3850.       return simplify_logical (x, last);
  3851.  
  3852.     case ABS:
  3853.       /* (abs (neg <foo>)) -> (abs <foo>) */
  3854.       if (GET_CODE (XEXP (x, 0)) == NEG)
  3855.     SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  3856.  
  3857.       /* If operand is something known to be positive, ignore the ABS.  */
  3858.       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
  3859.       || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
  3860.            <= HOST_BITS_PER_WIDE_INT)
  3861.           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
  3862.            & ((HOST_WIDE_INT) 1
  3863.               << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
  3864.           == 0)))
  3865.     return XEXP (x, 0);
  3866.  
  3867.  
  3868.       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
  3869.       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
  3870.     return gen_rtx_combine (NEG, mode, XEXP (x, 0));
  3871.  
  3872.       break;
  3873.  
  3874.     case FFS:
  3875.       /* (ffs (*_extend <X>)) = (ffs <X>) */
  3876.       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
  3877.       || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
  3878.     SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  3879.       break;
  3880.  
  3881.     case FLOAT:
  3882.       /* (float (sign_extend <X>)) = (float <X>).  */
  3883.       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
  3884.     SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  3885.       break;
  3886.  
  3887.     case ASHIFT:
  3888.     case LSHIFTRT:
  3889.     case ASHIFTRT:
  3890.     case ROTATE:
  3891.     case ROTATERT:
  3892.       /* If this is a shift by a constant amount, simplify it.  */
  3893.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  3894.     return simplify_shift_const (x, code, mode, XEXP (x, 0), 
  3895.                      INTVAL (XEXP (x, 1)));
  3896.  
  3897. #ifdef SHIFT_COUNT_TRUNCATED
  3898.       else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
  3899.     SUBST (XEXP (x, 1),
  3900.            force_to_mode (XEXP (x, 1), GET_MODE (x),
  3901.                   ((HOST_WIDE_INT) 1 
  3902.                    << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
  3903.                   - 1,
  3904.                   NULL_RTX, 0));
  3905. #endif
  3906.  
  3907.       break;
  3908.     }
  3909.  
  3910.   return x;
  3911. }
  3912.  
  3913. /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
  3914.  
  3915. static rtx
  3916. simplify_if_then_else (x)
  3917.      rtx x;
  3918. {
  3919.   enum machine_mode mode = GET_MODE (x);
  3920.   rtx cond = XEXP (x, 0);
  3921.   rtx true = XEXP (x, 1);
  3922.   rtx false = XEXP (x, 2);
  3923.   enum rtx_code true_code = GET_CODE (cond);
  3924.   int comparison_p = GET_RTX_CLASS (true_code) == '<';
  3925.   rtx temp;
  3926.   int i;
  3927.  
  3928.   /* Simplify storing of the truth value. */
  3929.   if (comparison_p && true == const_true_rtx && false == const0_rtx)
  3930.     return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
  3931.       
  3932.   /* Also when the truth value has to be reversed. */
  3933.   if (comparison_p && reversible_comparison_p (cond)
  3934.       && true == const0_rtx && false == const_true_rtx)
  3935.     return gen_binary (reverse_condition (true_code),
  3936.                mode, XEXP (cond, 0), XEXP (cond, 1));
  3937.  
  3938.   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
  3939.      in it is being compared against certain values.  Get the true and false
  3940.      comparisons and see if that says anything about the value of each arm.  */
  3941.  
  3942.   if (comparison_p && reversible_comparison_p (cond)
  3943.       && GET_CODE (XEXP (cond, 0)) == REG)
  3944.     {
  3945.       HOST_WIDE_INT nzb;
  3946.       rtx from = XEXP (cond, 0);
  3947.       enum rtx_code false_code = reverse_condition (true_code);
  3948.       rtx true_val = XEXP (cond, 1);
  3949.       rtx false_val = true_val;
  3950.       int swapped = 0;
  3951.  
  3952.       /* If FALSE_CODE is EQ, swap the codes and arms.  */
  3953.  
  3954.       if (false_code == EQ)
  3955.     {
  3956.       swapped = 1, true_code = EQ, false_code = NE;
  3957.       temp = true, true = false, false = temp;
  3958.     }
  3959.  
  3960.       /* If we are comparing against zero and the expression being tested has
  3961.      only a single bit that might be nonzero, that is its value when it is
  3962.      not equal to zero.  Similarly if it is known to be -1 or 0.  */
  3963.  
  3964.       if (true_code == EQ && true_val == const0_rtx
  3965.       && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
  3966.     false_code = EQ, false_val = GEN_INT (nzb);
  3967.       else if (true_code == EQ && true_val == const0_rtx
  3968.            && (num_sign_bit_copies (from, GET_MODE (from))
  3969.            == GET_MODE_BITSIZE (GET_MODE (from))))
  3970.     false_code = EQ, false_val = constm1_rtx;
  3971.  
  3972.       /* Now simplify an arm if we know the value of the register in the
  3973.      branch and it is used in the arm.  Be careful due to the potential
  3974.      of locally-shared RTL.  */
  3975.  
  3976.       if (reg_mentioned_p (from, true))
  3977.     true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
  3978.               pc_rtx, pc_rtx, 0, 0);
  3979.       if (reg_mentioned_p (from, false))
  3980.     false = subst (known_cond (copy_rtx (false), false_code,
  3981.                    from, false_val),
  3982.                pc_rtx, pc_rtx, 0, 0);
  3983.  
  3984.       SUBST (XEXP (x, 1), swapped ? false : true);
  3985.       SUBST (XEXP (x, 2), swapped ? true : false);
  3986.  
  3987.       true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
  3988.     }
  3989.  
  3990.   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
  3991.      reversed, do so to avoid needing two sets of patterns for
  3992.      subtract-and-branch insns.  Similarly if we have a constant in the true
  3993.      arm, the false arm is the same as the first operand of the comparison, or
  3994.      the false arm is more complicated than the true arm.  */
  3995.  
  3996.   if (comparison_p && reversible_comparison_p (cond)
  3997.       && (true == pc_rtx 
  3998.       || (CONSTANT_P (true)
  3999.           && GET_CODE (false) != CONST_INT && false != pc_rtx)
  4000.       || true == const0_rtx
  4001.       || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
  4002.           && GET_RTX_CLASS (GET_CODE (false)) != 'o')
  4003.       || (GET_CODE (true) == SUBREG
  4004.           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
  4005.           && GET_RTX_CLASS (GET_CODE (false)) != 'o')
  4006.       || reg_mentioned_p (true, false)
  4007.       || rtx_equal_p (false, XEXP (cond, 0))))
  4008.     {
  4009.       true_code = reverse_condition (true_code);
  4010.       SUBST (XEXP (x, 0),
  4011.          gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
  4012.              XEXP (cond, 1)));
  4013.  
  4014.       SUBST (XEXP (x, 1), false);
  4015.       SUBST (XEXP (x, 2), true);
  4016.  
  4017.       temp = true, true = false, false = temp, cond = XEXP (x, 0);
  4018.     }
  4019.  
  4020.   /* If the two arms are identical, we don't need the comparison.  */
  4021.  
  4022.   if (rtx_equal_p (true, false) && ! side_effects_p (cond))
  4023.     return true;
  4024.  
  4025.   /* Look for cases where we have (abs x) or (neg (abs X)).  */
  4026.  
  4027.   if (GET_MODE_CLASS (mode) == MODE_INT
  4028.       && GET_CODE (false) == NEG
  4029.       && rtx_equal_p (true, XEXP (false, 0))
  4030.       && comparison_p
  4031.       && rtx_equal_p (true, XEXP (cond, 0))
  4032.       && ! side_effects_p (true))
  4033.     switch (true_code)
  4034.       {
  4035.       case GT:
  4036.       case GE:
  4037.     return gen_unary (ABS, mode, mode, true);
  4038.       case LT:
  4039.       case LE:
  4040.     return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
  4041.       }
  4042.  
  4043.   /* Look for MIN or MAX.  */
  4044.  
  4045.   if ((! FLOAT_MODE_P (mode) || flag_fast_math)
  4046.       && comparison_p
  4047.       && rtx_equal_p (XEXP (cond, 0), true)
  4048.       && rtx_equal_p (XEXP (cond, 1), false)
  4049.       && ! side_effects_p (cond))
  4050.     switch (true_code)
  4051.       {
  4052.       case GE:
  4053.       case GT:
  4054.     return gen_binary (SMAX, mode, true, false);
  4055.       case LE:
  4056.       case LT:
  4057.     return gen_binary (SMIN, mode, true, false);
  4058.       case GEU:
  4059.       case GTU:
  4060.     return gen_binary (UMAX, mode, true, false);
  4061.       case LEU:
  4062.       case LTU:
  4063.     return gen_binary (UMIN, mode, true, false);
  4064.       }
  4065.   
  4066. #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
  4067.  
  4068.   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
  4069.      second operand is zero, this can be done as (OP Z (mult COND C2)) where
  4070.      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
  4071.      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
  4072.      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
  4073.      neither of the above, but it isn't worth checking for.  */
  4074.  
  4075.   if (comparison_p && mode != VOIDmode && ! side_effects_p (x))
  4076.     {
  4077.       rtx t = make_compound_operation (true, SET);
  4078.       rtx f = make_compound_operation (false, SET);
  4079.       rtx cond_op0 = XEXP (cond, 0);
  4080.       rtx cond_op1 = XEXP (cond, 1);
  4081.       enum rtx_code op, extend_op = NIL;
  4082.       enum machine_mode m = mode;
  4083.       rtx z = 0, c1;
  4084.  
  4085.       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
  4086.        || GET_CODE (t) == IOR || GET_CODE (t) == XOR
  4087.        || GET_CODE (t) == ASHIFT
  4088.        || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
  4089.       && rtx_equal_p (XEXP (t, 0), f))
  4090.     c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
  4091.  
  4092.       /* If an identity-zero op is commutative, check whether there
  4093.      would be a match if we swapped the operands. */
  4094.       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
  4095.         || GET_CODE (t) == XOR)
  4096.            && rtx_equal_p (XEXP (t, 1), f))
  4097.     c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
  4098.       else if (GET_CODE (t) == SIGN_EXTEND
  4099.            && (GET_CODE (XEXP (t, 0)) == PLUS
  4100.            || GET_CODE (XEXP (t, 0)) == MINUS
  4101.            || GET_CODE (XEXP (t, 0)) == IOR
  4102.            || GET_CODE (XEXP (t, 0)) == XOR
  4103.            || GET_CODE (XEXP (t, 0)) == ASHIFT
  4104.            || GET_CODE (XEXP (t, 0)) == LSHIFTRT
  4105.            || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
  4106.            && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
  4107.            && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
  4108.            && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
  4109.            && (num_sign_bit_copies (f, GET_MODE (f))
  4110.            > (GET_MODE_BITSIZE (mode)
  4111.               - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
  4112.     {
  4113.       c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
  4114.       extend_op = SIGN_EXTEND;
  4115.       m = GET_MODE (XEXP (t, 0));
  4116.     }
  4117.       else if (GET_CODE (t) == SIGN_EXTEND
  4118.            && (GET_CODE (XEXP (t, 0)) == PLUS
  4119.            || GET_CODE (XEXP (t, 0)) == IOR
  4120.            || GET_CODE (XEXP (t, 0)) == XOR)
  4121.            && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
  4122.            && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
  4123.            && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
  4124.            && (num_sign_bit_copies (f, GET_MODE (f))
  4125.            > (GET_MODE_BITSIZE (mode)
  4126.               - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
  4127.     {
  4128.       c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
  4129.       extend_op = SIGN_EXTEND;
  4130.       m = GET_MODE (XEXP (t, 0));
  4131.     }
  4132.       else if (GET_CODE (t) == ZERO_EXTEND
  4133.            && (GET_CODE (XEXP (t, 0)) == PLUS
  4134.            || GET_CODE (XEXP (t, 0)) == MINUS
  4135.            || GET_CODE (XEXP (t, 0)) == IOR
  4136.            || GET_CODE (XEXP (t, 0)) == XOR
  4137.            || GET_CODE (XEXP (t, 0)) == ASHIFT
  4138.            || GET_CODE (XEXP (t, 0)) == LSHIFTRT
  4139.            || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
  4140.            && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
  4141.            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  4142.            && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
  4143.            && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
  4144.            && ((nonzero_bits (f, GET_MODE (f))
  4145.             & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
  4146.            == 0))
  4147.     {
  4148.       c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
  4149.       extend_op = ZERO_EXTEND;
  4150.       m = GET_MODE (XEXP (t, 0));
  4151.     }
  4152.       else if (GET_CODE (t) == ZERO_EXTEND
  4153.            && (GET_CODE (XEXP (t, 0)) == PLUS
  4154.            || GET_CODE (XEXP (t, 0)) == IOR
  4155.            || GET_CODE (XEXP (t, 0)) == XOR)
  4156.            && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
  4157.            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  4158.            && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
  4159.            && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
  4160.            && ((nonzero_bits (f, GET_MODE (f))
  4161.             & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
  4162.            == 0))
  4163.     {
  4164.       c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
  4165.       extend_op = ZERO_EXTEND;
  4166.       m = GET_MODE (XEXP (t, 0));
  4167.     }
  4168.       
  4169.       if (z)
  4170.     {
  4171.       temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
  4172.             pc_rtx, pc_rtx, 0, 0);
  4173.       temp = gen_binary (MULT, m, temp,
  4174.                  gen_binary (MULT, m, c1, const_true_rtx));
  4175.       temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
  4176.       temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
  4177.  
  4178.       if (extend_op != NIL)
  4179.         temp = gen_unary (extend_op, mode, m, temp);
  4180.  
  4181.       return temp;
  4182.     }
  4183.     }
  4184. #endif
  4185.  
  4186.   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
  4187.      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
  4188.      negation of a single bit, we can convert this operation to a shift.  We
  4189.      can actually do this more generally, but it doesn't seem worth it.  */
  4190.  
  4191.   if (true_code == NE && XEXP (cond, 1) == const0_rtx
  4192.       && false == const0_rtx && GET_CODE (true) == CONST_INT
  4193.       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
  4194.        && (i = exact_log2 (INTVAL (true))) >= 0)
  4195.       || ((num_sign_bit_copies (XEXP (cond, 0), mode)
  4196.            == GET_MODE_BITSIZE (mode))
  4197.           && (i = exact_log2 (- INTVAL (true))) >= 0)))
  4198.     return
  4199.       simplify_shift_const (NULL_RTX, ASHIFT, mode,
  4200.                 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
  4201.  
  4202.   return x;
  4203. }
  4204.  
  4205. /* Simplify X, a SET expression.  Return the new expression.  */
  4206.  
  4207. static rtx
  4208. simplify_set (x)
  4209.      rtx x;
  4210. {
  4211.   rtx src = SET_SRC (x);
  4212.   rtx dest = SET_DEST (x);
  4213.   enum machine_mode mode
  4214.     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
  4215.   rtx other_insn;
  4216.   rtx *cc_use;
  4217.  
  4218.   /* (set (pc) (return)) gets written as (return).  */
  4219.   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
  4220.     return src;
  4221.  
  4222.   /* Now that we know for sure which bits of SRC we are using, see if we can
  4223.      simplify the expression for the object knowing that we only need the
  4224.      low-order bits.  */
  4225.  
  4226.   if (GET_MODE_CLASS (mode) == MODE_INT)
  4227.     src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
  4228.  
  4229.   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
  4230.      the comparison result and try to simplify it unless we already have used
  4231.      undobuf.other_insn.  */
  4232.   if ((GET_CODE (src) == COMPARE
  4233. #ifdef HAVE_cc0
  4234.        || dest == cc0_rtx
  4235. #endif
  4236.        )
  4237.       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
  4238.       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
  4239.       && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
  4240.       && rtx_equal_p (XEXP (*cc_use, 0), dest))
  4241.     {
  4242.       enum rtx_code old_code = GET_CODE (*cc_use);
  4243.       enum rtx_code new_code;
  4244.       rtx op0, op1;
  4245.       int other_changed = 0;
  4246.       enum machine_mode compare_mode = GET_MODE (dest);
  4247.  
  4248.       if (GET_CODE (src) == COMPARE)
  4249.     op0 = XEXP (src, 0), op1 = XEXP (src, 1);
  4250.       else
  4251.     op0 = src, op1 = const0_rtx;
  4252.  
  4253.       /* Simplify our comparison, if possible.  */
  4254.       new_code = simplify_comparison (old_code, &op0, &op1);
  4255.  
  4256. #ifdef EXTRA_CC_MODES
  4257.       /* If this machine has CC modes other than CCmode, check to see if we
  4258.      need to use a different CC mode here.  */
  4259.       compare_mode = SELECT_CC_MODE (new_code, op0, op1);
  4260. #endif /* EXTRA_CC_MODES */
  4261.  
  4262. #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
  4263.       /* If the mode changed, we have to change SET_DEST, the mode in the
  4264.      compare, and the mode in the place SET_DEST is used.  If SET_DEST is
  4265.      a hard register, just build new versions with the proper mode.  If it
  4266.      is a pseudo, we lose unless it is only time we set the pseudo, in
  4267.      which case we can safely change its mode.  */
  4268.       if (compare_mode != GET_MODE (dest))
  4269.     {
  4270.       int regno = REGNO (dest);
  4271.       rtx new_dest = gen_rtx (REG, compare_mode, regno);
  4272.  
  4273.       if (regno < FIRST_PSEUDO_REGISTER
  4274.           || (reg_n_sets[regno] == 1 && ! REG_USERVAR_P (dest)))
  4275.         {
  4276.           if (regno >= FIRST_PSEUDO_REGISTER)
  4277.         SUBST (regno_reg_rtx[regno], new_dest);
  4278.  
  4279.           SUBST (SET_DEST (x), new_dest);
  4280.           SUBST (XEXP (*cc_use, 0), new_dest);
  4281.           other_changed = 1;
  4282.  
  4283.           dest = new_dest;
  4284.         }
  4285.     }
  4286. #endif
  4287.  
  4288.       /* If the code changed, we have to build a new comparison in
  4289.      undobuf.other_insn.  */
  4290.       if (new_code != old_code)
  4291.     {
  4292.       unsigned HOST_WIDE_INT mask;
  4293.  
  4294.       SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
  4295.                        dest, const0_rtx));
  4296.  
  4297.       /* If the only change we made was to change an EQ into an NE or
  4298.          vice versa, OP0 has only one bit that might be nonzero, and OP1
  4299.          is zero, check if changing the user of the condition code will
  4300.          produce a valid insn.  If it won't, we can keep the original code
  4301.          in that insn by surrounding our operation with an XOR.  */
  4302.  
  4303.       if (((old_code == NE && new_code == EQ)
  4304.            || (old_code == EQ && new_code == NE))
  4305.           && ! other_changed && op1 == const0_rtx
  4306.           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
  4307.           && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
  4308.         {
  4309.           rtx pat = PATTERN (other_insn), note = 0;
  4310.           int scratches;
  4311.  
  4312.           if ((recog_for_combine (&pat, other_insn, ¬e, &scratches) < 0
  4313.            && ! check_asm_operands (pat)))
  4314.         {
  4315.           PUT_CODE (*cc_use, old_code);
  4316.           other_insn = 0;
  4317.  
  4318.           op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
  4319.         }
  4320.         }
  4321.  
  4322.       other_changed = 1;
  4323.     }
  4324.  
  4325.       if (other_changed)
  4326.     undobuf.other_insn = other_insn;
  4327.  
  4328. #ifdef HAVE_cc0
  4329.       /* If we are now comparing against zero, change our source if
  4330.      needed.  If we do not use cc0, we always have a COMPARE.  */
  4331.       if (op1 == const0_rtx && dest == cc0_rtx)
  4332.     {
  4333.       SUBST (SET_SRC (x), op0);
  4334.       src = op0;
  4335.     }
  4336.       else
  4337. #endif
  4338.  
  4339.       /* Otherwise, if we didn't previously have a COMPARE in the
  4340.      correct mode, we need one.  */
  4341.       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
  4342.     {
  4343.       SUBST (SET_SRC (x),
  4344.          gen_rtx_combine (COMPARE, compare_mode, op0, op1));
  4345.       src = SET_SRC (x);
  4346.     }
  4347.       else
  4348.     {
  4349.       /* Otherwise, update the COMPARE if needed.  */
  4350.       SUBST (XEXP (src, 0), op0);
  4351.       SUBST (XEXP (src, 1), op1);
  4352.     }
  4353.     }
  4354.   else
  4355.     {
  4356.       /* Get SET_SRC in a form where we have placed back any
  4357.      compound expressions.  Then do the checks below.  */
  4358.       src = make_compound_operation (src, SET);
  4359.       SUBST (SET_SRC (x), src);
  4360.     }
  4361.  
  4362.   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
  4363.      and X being a REG or (subreg (reg)), we may be able to convert this to
  4364.      (set (subreg:m2 x) (op)). 
  4365.  
  4366.      We can always do this if M1 is narrower than M2 because that means that
  4367.      we only care about the low bits of the result.
  4368.  
  4369.      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
  4370.      perform a narrower operation that requested since the high-order bits will
  4371.      be undefined.  On machine where it is defined, this transformation is safe
  4372.      as long as M1 and M2 have the same number of words.  */
  4373.  
  4374.   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
  4375.       && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
  4376.       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
  4377.        / UNITS_PER_WORD)
  4378.       == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
  4379.            + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
  4380. #ifndef WORD_REGISTER_OPERATIONS
  4381.       && (GET_MODE_SIZE (GET_MODE (src))
  4382.       < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
  4383. #endif
  4384. #ifdef CLASS_CANNOT_CHANGE_SIZE
  4385.       && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
  4386.         && (TEST_HARD_REG_BIT
  4387.         (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
  4388.          REGNO (dest)))
  4389.         && (GET_MODE_SIZE (GET_MODE (src))
  4390.         != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
  4391. #endif                  
  4392.       && (GET_CODE (dest) == REG
  4393.       || (GET_CODE (dest) == SUBREG
  4394.           && GET_CODE (SUBREG_REG (dest)) == REG)))
  4395.     {
  4396.       SUBST (SET_DEST (x),
  4397.          gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
  4398.                       dest));
  4399.       SUBST (SET_SRC (x), SUBREG_REG (src));
  4400.  
  4401.       src = SET_SRC (x), dest = SET_DEST (x);
  4402.     }
  4403.  
  4404. #ifdef LOAD_EXTEND_OP
  4405.   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
  4406.      would require a paradoxical subreg.  Replace the subreg with a
  4407.      zero_extend to avoid the reload that would otherwise be required. */
  4408.  
  4409.   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
  4410.       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
  4411.       && SUBREG_WORD (src) == 0
  4412.       && (GET_MODE_SIZE (GET_MODE (src))
  4413.       > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
  4414.       && GET_CODE (SUBREG_REG (src)) == MEM)
  4415.     {
  4416.       SUBST (SET_SRC (x),
  4417.          gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
  4418.                   GET_MODE (src), XEXP (src, 0)));
  4419.  
  4420.       src = SET_SRC (x);
  4421.     }
  4422. #endif
  4423.  
  4424.   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
  4425.      are comparing an item known to be 0 or -1 against 0, use a logical
  4426.      operation instead. Check for one of the arms being an IOR of the other
  4427.      arm with some value.  We compute three terms to be IOR'ed together.  In
  4428.      practice, at most two will be nonzero.  Then we do the IOR's.  */
  4429.  
  4430.   if (GET_CODE (dest) != PC
  4431.       && GET_CODE (src) == IF_THEN_ELSE
  4432.       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
  4433.       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
  4434.       && XEXP (XEXP (src, 0), 1) == const0_rtx
  4435.       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
  4436. #ifdef HAVE_conditional_move
  4437.       && ! can_conditionally_move_p (GET_MODE (src))
  4438. #endif
  4439.       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
  4440.                    GET_MODE (XEXP (XEXP (src, 0), 0)))
  4441.       == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
  4442.       && ! side_effects_p (src))
  4443.     {
  4444.       rtx true = (GET_CODE (XEXP (src, 0)) == NE
  4445.               ? XEXP (src, 1) : XEXP (src, 2));
  4446.       rtx false = (GET_CODE (XEXP (src, 0)) == NE
  4447.            ? XEXP (src, 2) : XEXP (src, 1));
  4448.       rtx term1 = const0_rtx, term2, term3;
  4449.  
  4450.       if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
  4451.     term1 = false, true = XEXP (true, 1), false = const0_rtx;
  4452.       else if (GET_CODE (true) == IOR
  4453.            && rtx_equal_p (XEXP (true, 1), false))
  4454.     term1 = false, true = XEXP (true, 0), false = const0_rtx;
  4455.       else if (GET_CODE (false) == IOR
  4456.            && rtx_equal_p (XEXP (false, 0), true))
  4457.     term1 = true, false = XEXP (false, 1), true = const0_rtx;
  4458.       else if (GET_CODE (false) == IOR
  4459.            && rtx_equal_p (XEXP (false, 1), true))
  4460.     term1 = true, false = XEXP (false, 0), true = const0_rtx;
  4461.  
  4462.       term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
  4463.       term3 = gen_binary (AND, GET_MODE (src),
  4464.               gen_unary (NOT, GET_MODE (src), GET_MODE (src),
  4465.                      XEXP (XEXP (src, 0), 0)),
  4466.               false);
  4467.  
  4468.       SUBST (SET_SRC (x),
  4469.          gen_binary (IOR, GET_MODE (src),
  4470.              gen_binary (IOR, GET_MODE (src), term1, term2),
  4471.              term3));
  4472.  
  4473.       src = SET_SRC (x);
  4474.     }
  4475.  
  4476.   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
  4477.      whole thing fail.  */
  4478.   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
  4479.     return src;
  4480.   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
  4481.     return dest;
  4482.   else
  4483.     /* Convert this into a field assignment operation, if possible.  */
  4484.     return make_field_assignment (x);
  4485. }
  4486.  
  4487. /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
  4488.    result.  LAST is nonzero if this is the last retry.  */
  4489.  
  4490. static rtx
  4491. simplify_logical (x, last)
  4492.      rtx x;
  4493.      int last;
  4494. {
  4495.   enum machine_mode mode = GET_MODE (x);
  4496.   rtx op0 = XEXP (x, 0);
  4497.   rtx op1 = XEXP (x, 1);
  4498.  
  4499.   switch (GET_CODE (x))
  4500.     {
  4501.     case AND:
  4502.       /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
  4503.      insn (and may simplify more).  */
  4504.       if (GET_CODE (op0) == XOR
  4505.       && rtx_equal_p (XEXP (op0, 0), op1)
  4506.       && ! side_effects_p (op1))
  4507.     x = gen_binary (AND, mode,
  4508.             gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
  4509.  
  4510.       if (GET_CODE (op0) == XOR
  4511.       && rtx_equal_p (XEXP (op0, 1), op1)
  4512.       && ! side_effects_p (op1))
  4513.     x = gen_binary (AND, mode,
  4514.             gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
  4515.  
  4516.       /* Similarly for (~ (A ^ B)) & A.  */
  4517.       if (GET_CODE (op0) == NOT
  4518.       && GET_CODE (XEXP (op0, 0)) == XOR
  4519.       && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
  4520.       && ! side_effects_p (op1))
  4521.     x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
  4522.  
  4523.       if (GET_CODE (op0) == NOT
  4524.       && GET_CODE (XEXP (op0, 0)) == XOR
  4525.       && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
  4526.       && ! side_effects_p (op1))
  4527.     x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
  4528.  
  4529.       if (GET_CODE (op1) == CONST_INT)
  4530.     {
  4531.       x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
  4532.  
  4533.       /* If we have (ior (and (X C1) C2)) and the next restart would be
  4534.          the last, simplify this by making C1 as small as possible
  4535.          and then exit. */
  4536.       if (last
  4537.           && GET_CODE (x) == IOR && GET_CODE (op0) == AND
  4538.           && GET_CODE (XEXP (op0, 1)) == CONST_INT
  4539.           && GET_CODE (op1) == CONST_INT)
  4540.         return gen_binary (IOR, mode,
  4541.                    gen_binary (AND, mode, XEXP (op0, 0),
  4542.                        GEN_INT (INTVAL (XEXP (op0, 1))
  4543.                             & ~ INTVAL (op1))), op1);
  4544.  
  4545.       if (GET_CODE (x) != AND)
  4546.         return x;
  4547.  
  4548.       if (GET_RTX_CLASS (GET_CODE (x)) == 'c' 
  4549.           || GET_RTX_CLASS (GET_CODE (x)) == '2')
  4550.         op0 = XEXP (x, 0), op1 = XEXP (x, 1);
  4551.     }
  4552.  
  4553.       /* Convert (A | B) & A to A.  */
  4554.       if (GET_CODE (op0) == IOR
  4555.       && (rtx_equal_p (XEXP (op0, 0), op1)
  4556.           || rtx_equal_p (XEXP (op0, 1), op1))
  4557.       && ! side_effects_p (XEXP (op0, 0))
  4558.       && ! side_effects_p (XEXP (op0, 1)))
  4559.     return op1;
  4560.  
  4561.       /* In the following group of tests (and those in case IOR below),
  4562.      we start with some combination of logical operations and apply
  4563.      the distributive law followed by the inverse distributive law.
  4564.      Most of the time, this results in no change.  However, if some of
  4565.      the operands are the same or inverses of each other, simplifications
  4566.      will result.
  4567.  
  4568.      For example, (and (ior A B) (not B)) can occur as the result of
  4569.      expanding a bit field assignment.  When we apply the distributive
  4570.      law to this, we get (ior (and (A (not B))) (and (B (not B)))),
  4571.      which then simplifies to (and (A (not B))). 
  4572.  
  4573.      If we have (and (ior A B) C), apply the distributive law and then
  4574.      the inverse distributive law to see if things simplify.  */
  4575.  
  4576.       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
  4577.     {
  4578.       x = apply_distributive_law
  4579.         (gen_binary (GET_CODE (op0), mode,
  4580.              gen_binary (AND, mode, XEXP (op0, 0), op1),
  4581.              gen_binary (AND, mode, XEXP (op0, 1), op1)));
  4582.       if (GET_CODE (x) != AND)
  4583.         return x;
  4584.     }
  4585.  
  4586.       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
  4587.     return apply_distributive_law
  4588.       (gen_binary (GET_CODE (op1), mode,
  4589.                gen_binary (AND, mode, XEXP (op1, 0), op0),
  4590.                gen_binary (AND, mode, XEXP (op1, 1), op0)));
  4591.  
  4592.       /* Similarly, taking advantage of the fact that
  4593.      (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
  4594.  
  4595.       if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
  4596.     return apply_distributive_law
  4597.       (gen_binary (XOR, mode,
  4598.                gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
  4599.                gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
  4600.                                 
  4601.       else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
  4602.     return apply_distributive_law
  4603.       (gen_binary (XOR, mode,
  4604.                gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
  4605.                gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
  4606.       break;
  4607.  
  4608.     case IOR:
  4609.       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
  4610.       if (GET_CODE (op1) == CONST_INT
  4611.       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  4612.       && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
  4613.     return op1;
  4614.  
  4615.       /* Convert (A & B) | A to A.  */
  4616.       if (GET_CODE (op0) == AND
  4617.       && (rtx_equal_p (XEXP (op0, 0), op1)
  4618.           || rtx_equal_p (XEXP (op0, 1), op1))
  4619.       && ! side_effects_p (XEXP (op0, 0))
  4620.       && ! side_effects_p (XEXP (op0, 1)))
  4621.     return op1;
  4622.  
  4623.       /* If we have (ior (and A B) C), apply the distributive law and then
  4624.      the inverse distributive law to see if things simplify.  */
  4625.  
  4626.       if (GET_CODE (op0) == AND)
  4627.     {
  4628.       x = apply_distributive_law
  4629.         (gen_binary (AND, mode,
  4630.              gen_binary (IOR, mode, XEXP (op0, 0), op1),
  4631.              gen_binary (IOR, mode, XEXP (op0, 1), op1)));
  4632.  
  4633.       if (GET_CODE (x) != IOR)
  4634.         return x;
  4635.     }
  4636.  
  4637.       if (GET_CODE (op1) == AND)
  4638.     {
  4639.       x = apply_distributive_law
  4640.         (gen_binary (AND, mode,
  4641.              gen_binary (IOR, mode, XEXP (op1, 0), op0),
  4642.              gen_binary (IOR, mode, XEXP (op1, 1), op0)));
  4643.  
  4644.       if (GET_CODE (x) != IOR)
  4645.         return x;
  4646.     }
  4647.  
  4648.       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
  4649.      mode size to (rotate A CX).  */
  4650.  
  4651.       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
  4652.        || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
  4653.       && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
  4654.       && GET_CODE (XEXP (op0, 1)) == CONST_INT
  4655.       && GET_CODE (XEXP (op1, 1)) == CONST_INT
  4656.       && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
  4657.           == GET_MODE_BITSIZE (mode)))
  4658.     return gen_rtx (ROTATE, mode, XEXP (op0, 0),
  4659.             (GET_CODE (op0) == ASHIFT
  4660.              ? XEXP (op0, 1) : XEXP (op1, 1)));
  4661.  
  4662.       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
  4663.      a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
  4664.      does not affect any of the bits in OP1, it can really be done
  4665.      as a PLUS and we can associate.  We do this by seeing if OP1
  4666.      can be safely shifted left C bits.  */
  4667.       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
  4668.       && GET_CODE (XEXP (op0, 0)) == PLUS
  4669.       && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
  4670.       && GET_CODE (XEXP (op0, 1)) == CONST_INT
  4671.       && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
  4672.     {
  4673.       int count = INTVAL (XEXP (op0, 1));
  4674.       HOST_WIDE_INT mask = INTVAL (op1) << count;
  4675.  
  4676.       if (mask >> count == INTVAL (op1)
  4677.           && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
  4678.         {
  4679.           SUBST (XEXP (XEXP (op0, 0), 1),
  4680.              GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
  4681.           return op0;
  4682.         }
  4683.     }
  4684.       break;
  4685.  
  4686.     case XOR:
  4687.       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
  4688.      Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
  4689.      (NOT y).  */
  4690.       {
  4691.     int num_negated = 0;
  4692.  
  4693.     if (GET_CODE (op0) == NOT)
  4694.       num_negated++, op0 = XEXP (op0, 0);
  4695.     if (GET_CODE (op1) == NOT)
  4696.       num_negated++, op1 = XEXP (op1, 0);
  4697.  
  4698.     if (num_negated == 2)
  4699.       {
  4700.         SUBST (XEXP (x, 0), op0);
  4701.         SUBST (XEXP (x, 1), op1);
  4702.       }
  4703.     else if (num_negated == 1)
  4704.       return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
  4705.       }
  4706.  
  4707.       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
  4708.      correspond to a machine insn or result in further simplifications
  4709.      if B is a constant.  */
  4710.  
  4711.       if (GET_CODE (op0) == AND
  4712.       && rtx_equal_p (XEXP (op0, 1), op1)
  4713.       && ! side_effects_p (op1))
  4714.     return gen_binary (AND, mode,
  4715.                gen_unary (NOT, mode, mode, XEXP (op0, 0)),
  4716.                op1);
  4717.  
  4718.       else if (GET_CODE (op0) == AND
  4719.            && rtx_equal_p (XEXP (op0, 0), op1)
  4720.            && ! side_effects_p (op1))
  4721.     return gen_binary (AND, mode,
  4722.                gen_unary (NOT, mode, mode, XEXP (op0, 1)),
  4723.                op1);
  4724.  
  4725. #if STORE_FLAG_VALUE == 1
  4726.       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
  4727.      comparison.  */
  4728.       if (op1 == const1_rtx
  4729.       && GET_RTX_CLASS (GET_CODE (op0)) == '<'
  4730.       && reversible_comparison_p (op0))
  4731.     return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
  4732.                 mode, XEXP (op0, 0), XEXP (op0, 1));
  4733.  
  4734.       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
  4735.      is (lt foo (const_int 0)), so we can perform the above
  4736.      simplification.  */
  4737.  
  4738.       if (op1 == const1_rtx
  4739.       && GET_CODE (op0) == LSHIFTRT
  4740.       && GET_CODE (XEXP (op0, 1)) == CONST_INT
  4741.       && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
  4742.     return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
  4743. #endif
  4744.  
  4745.       /* (xor (comparison foo bar) (const_int sign-bit))
  4746.      when STORE_FLAG_VALUE is the sign bit.  */
  4747.       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  4748.       && (STORE_FLAG_VALUE
  4749.           == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
  4750.       && op1 == const_true_rtx
  4751.       && GET_RTX_CLASS (GET_CODE (op0)) == '<'
  4752.       && reversible_comparison_p (op0))
  4753.     return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
  4754.                 mode, XEXP (op0, 0), XEXP (op0, 1));
  4755.       break;
  4756.     }
  4757.  
  4758.   return x;
  4759. }
  4760.  
  4761. /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
  4762.    operations" because they can be replaced with two more basic operations.
  4763.    ZERO_EXTEND is also considered "compound" because it can be replaced with
  4764.    an AND operation, which is simpler, though only one operation.
  4765.  
  4766.    The function expand_compound_operation is called with an rtx expression
  4767.    and will convert it to the appropriate shifts and AND operations, 
  4768.    simplifying at each stage.
  4769.  
  4770.    The function make_compound_operation is called to convert an expression
  4771.    consisting of shifts and ANDs into the equivalent compound expression.
  4772.    It is the inverse of this function, loosely speaking.  */
  4773.  
  4774. static rtx
  4775. expand_compound_operation (x)
  4776.      rtx x;
  4777. {
  4778.   int pos = 0, len;
  4779.   int unsignedp = 0;
  4780.   int modewidth;
  4781.   rtx tem;
  4782.  
  4783.   switch (GET_CODE (x))
  4784.     {
  4785.     case ZERO_EXTEND:
  4786.       unsignedp = 1;
  4787.     case SIGN_EXTEND:
  4788.       /* We can't necessarily use a const_int for a multiword mode;
  4789.      it depends on implicitly extending the value.
  4790.      Since we don't know the right way to extend it,
  4791.      we can't tell whether the implicit way is right.
  4792.  
  4793.      Even for a mode that is no wider than a const_int,
  4794.      we can't win, because we need to sign extend one of its bits through
  4795.      the rest of it, and we don't know which bit.  */
  4796.       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  4797.     return x;
  4798.  
  4799.       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
  4800.      (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
  4801.      because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
  4802.      reloaded. If not for that, MEM's would very rarely be safe.
  4803.  
  4804.      Reject MODEs bigger than a word, because we might not be able
  4805.      to reference a two-register group starting with an arbitrary register
  4806.      (and currently gen_lowpart might crash for a SUBREG).  */
  4807.   
  4808.       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
  4809.     return x;
  4810.  
  4811.       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
  4812.       /* If the inner object has VOIDmode (the only way this can happen
  4813.      is if it is a ASM_OPERANDS), we can't do anything since we don't
  4814.      know how much masking to do.  */
  4815.       if (len == 0)
  4816.     return x;
  4817.  
  4818.       break;
  4819.  
  4820.     case ZERO_EXTRACT:
  4821.       unsignedp = 1;
  4822.     case SIGN_EXTRACT:
  4823.       /* If the operand is a CLOBBER, just return it.  */
  4824.       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
  4825.     return XEXP (x, 0);
  4826.  
  4827.       if (GET_CODE (XEXP (x, 1)) != CONST_INT
  4828.       || GET_CODE (XEXP (x, 2)) != CONST_INT
  4829.       || GET_MODE (XEXP (x, 0)) == VOIDmode)
  4830.     return x;
  4831.  
  4832.       len = INTVAL (XEXP (x, 1));
  4833.       pos = INTVAL (XEXP (x, 2));
  4834.  
  4835.       /* If this goes outside the object being extracted, replace the object
  4836.      with a (use (mem ...)) construct that only combine understands
  4837.      and is used only for this purpose.  */
  4838.       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
  4839.     SUBST (XEXP (x, 0), gen_rtx (USE, GET_MODE (x), XEXP (x, 0)));
  4840.  
  4841.       if (BITS_BIG_ENDIAN)
  4842.     pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
  4843.  
  4844.       break;
  4845.  
  4846.     default:
  4847.       return x;
  4848.     }
  4849.  
  4850.   /* If we reach here, we want to return a pair of shifts.  The inner
  4851.      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
  4852.      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
  4853.      logical depending on the value of UNSIGNEDP.
  4854.  
  4855.      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
  4856.      converted into an AND of a shift.
  4857.  
  4858.      We must check for the case where the left shift would have a negative
  4859.      count.  This can happen in a case like (x >> 31) & 255 on machines
  4860.      that can't shift by a constant.  On those machines, we would first
  4861.      combine the shift with the AND to produce a variable-position 
  4862.      extraction.  Then the constant of 31 would be substituted in to produce
  4863.      a such a position.  */
  4864.  
  4865.   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
  4866.   if (modewidth >= pos - len)
  4867.     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
  4868.                 GET_MODE (x),
  4869.                 simplify_shift_const (NULL_RTX, ASHIFT,
  4870.                               GET_MODE (x),
  4871.                               XEXP (x, 0),
  4872.                               modewidth - pos - len),
  4873.                 modewidth - len);
  4874.  
  4875.   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
  4876.     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
  4877.                   simplify_shift_const (NULL_RTX, LSHIFTRT,
  4878.                             GET_MODE (x),
  4879.                             XEXP (x, 0), pos),
  4880.                   ((HOST_WIDE_INT) 1 << len) - 1);
  4881.   else
  4882.     /* Any other cases we can't handle.  */
  4883.     return x;
  4884.     
  4885.  
  4886.   /* If we couldn't do this for some reason, return the original
  4887.      expression.  */
  4888.   if (GET_CODE (tem) == CLOBBER)
  4889.     return x;
  4890.  
  4891.   return tem;
  4892. }
  4893.  
  4894. /* X is a SET which contains an assignment of one object into
  4895.    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
  4896.    or certain SUBREGS). If possible, convert it into a series of
  4897.    logical operations.
  4898.  
  4899.    We half-heartedly support variable positions, but do not at all
  4900.    support variable lengths.  */
  4901.  
  4902. static rtx
  4903. expand_field_assignment (x)
  4904.      rtx x;
  4905. {
  4906.   rtx inner;
  4907.   rtx pos;            /* Always counts from low bit. */
  4908.   int len;
  4909.   rtx mask;
  4910.   enum machine_mode compute_mode;
  4911.  
  4912.   /* Loop until we find something we can't simplify.  */
  4913.   while (1)
  4914.     {
  4915.       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
  4916.       && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
  4917.     {
  4918.       inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
  4919.       len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
  4920.       pos = const0_rtx;
  4921.     }
  4922.       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
  4923.            && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
  4924.     {
  4925.       inner = XEXP (SET_DEST (x), 0);
  4926.       len = INTVAL (XEXP (SET_DEST (x), 1));
  4927.       pos = XEXP (SET_DEST (x), 2);
  4928.  
  4929.       /* If the position is constant and spans the width of INNER,
  4930.          surround INNER  with a USE to indicate this.  */
  4931.       if (GET_CODE (pos) == CONST_INT
  4932.           && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
  4933.         inner = gen_rtx (USE, GET_MODE (SET_DEST (x)), inner);
  4934.  
  4935.       if (BITS_BIG_ENDIAN)
  4936.         {
  4937.           if (GET_CODE (pos) == CONST_INT)
  4938.         pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
  4939.                    - INTVAL (pos));
  4940.           else if (GET_CODE (pos) == MINUS
  4941.                && GET_CODE (XEXP (pos, 1)) == CONST_INT
  4942.                && (INTVAL (XEXP (pos, 1))
  4943.                == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
  4944.         /* If position is ADJUST - X, new position is X.  */
  4945.         pos = XEXP (pos, 0);
  4946.           else
  4947.         pos = gen_binary (MINUS, GET_MODE (pos),
  4948.                   GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
  4949.                        - len),
  4950.                   pos);
  4951.         }
  4952.     }
  4953.  
  4954.       /* A SUBREG between two modes that occupy the same numbers of words
  4955.      can be done by moving the SUBREG to the source.  */
  4956.       else if (GET_CODE (SET_DEST (x)) == SUBREG
  4957.            && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
  4958.              + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
  4959.            == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
  4960.             + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
  4961.     {
  4962.       x = gen_rtx (SET, VOIDmode, SUBREG_REG (SET_DEST (x)),
  4963.                gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_DEST (x))),
  4964.                         SET_SRC (x)));
  4965.       continue;
  4966.     }
  4967.       else
  4968.     break;
  4969.  
  4970.       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
  4971.     inner = SUBREG_REG (inner);
  4972.  
  4973.       compute_mode = GET_MODE (inner);
  4974.  
  4975.       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
  4976.       if (len < HOST_BITS_PER_WIDE_INT)
  4977.     mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
  4978.       else
  4979.     break;
  4980.  
  4981.       /* Now compute the equivalent expression.  Make a copy of INNER
  4982.      for the SET_DEST in case it is a MEM into which we will substitute;
  4983.      we don't want shared RTL in that case.  */
  4984.       x = gen_rtx (SET, VOIDmode, copy_rtx (inner),
  4985.            gen_binary (IOR, compute_mode,
  4986.                    gen_binary (AND, compute_mode,
  4987.                        gen_unary (NOT, compute_mode,
  4988.                               compute_mode,
  4989.                               gen_binary (ASHIFT,
  4990.                                   compute_mode,
  4991.                                   mask, pos)),
  4992.                        inner),
  4993.                    gen_binary (ASHIFT, compute_mode,
  4994.                        gen_binary (AND, compute_mode,
  4995.                                gen_lowpart_for_combine
  4996.                                (compute_mode,
  4997.                             SET_SRC (x)),
  4998.                                mask),
  4999.                        pos)));
  5000.     }
  5001.  
  5002.   return x;
  5003. }
  5004.  
  5005. /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
  5006.    it is an RTX that represents a variable starting position; otherwise,
  5007.    POS is the (constant) starting bit position (counted from the LSB).
  5008.  
  5009.    INNER may be a USE.  This will occur when we started with a bitfield
  5010.    that went outside the boundary of the object in memory, which is
  5011.    allowed on most machines.  To isolate this case, we produce a USE
  5012.    whose mode is wide enough and surround the MEM with it.  The only
  5013.    code that understands the USE is this routine.  If it is not removed,
  5014.    it will cause the resulting insn not to match.
  5015.  
  5016.    UNSIGNEDP is non-zero for an unsigned reference and zero for a 
  5017.    signed reference.
  5018.  
  5019.    IN_DEST is non-zero if this is a reference in the destination of a
  5020.    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If non-zero,
  5021.    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
  5022.    be used.
  5023.  
  5024.    IN_COMPARE is non-zero if we are in a COMPARE.  This means that a
  5025.    ZERO_EXTRACT should be built even for bits starting at bit 0.
  5026.  
  5027.    MODE is the desired mode of the result (if IN_DEST == 0).  */
  5028.  
  5029. static rtx
  5030. make_extraction (mode, inner, pos, pos_rtx, len,
  5031.          unsignedp, in_dest, in_compare)
  5032.      enum machine_mode mode;
  5033.      rtx inner;
  5034.      int pos;
  5035.      rtx pos_rtx;
  5036.      int len;
  5037.      int unsignedp;
  5038.      int in_dest, in_compare;
  5039. {
  5040.   /* This mode describes the size of the storage area
  5041.      to fetch the overall value from.  Within that, we
  5042.      ignore the POS lowest bits, etc.  */
  5043.   enum machine_mode is_mode = GET_MODE (inner);
  5044.   enum machine_mode inner_mode;
  5045.   enum machine_mode wanted_mem_mode = byte_mode;
  5046.   enum machine_mode pos_mode = word_mode;
  5047.   enum machine_mode extraction_mode = word_mode;
  5048.   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
  5049.   int spans_byte = 0;
  5050.   rtx new = 0;
  5051.   rtx orig_pos_rtx = pos_rtx;
  5052.   int orig_pos;
  5053.  
  5054.   /* Get some information about INNER and get the innermost object.  */
  5055.   if (GET_CODE (inner) == USE)
  5056.     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
  5057.     /* We don't need to adjust the position because we set up the USE
  5058.        to pretend that it was a full-word object.  */
  5059.     spans_byte = 1, inner = XEXP (inner, 0);
  5060.   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
  5061.     {
  5062.       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
  5063.      consider just the QI as the memory to extract from.
  5064.      The subreg adds or removes high bits; its mode is
  5065.      irrelevant to the meaning of this extraction,
  5066.      since POS and LEN count from the lsb.  */
  5067.       if (GET_CODE (SUBREG_REG (inner)) == MEM)
  5068.     is_mode = GET_MODE (SUBREG_REG (inner));
  5069.       inner = SUBREG_REG (inner);
  5070.     }
  5071.  
  5072.   inner_mode = GET_MODE (inner);
  5073.  
  5074.   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
  5075.     pos = INTVAL (pos_rtx), pos_rtx = 0;
  5076.  
  5077.   /* See if this can be done without an extraction.  We never can if the
  5078.      width of the field is not the same as that of some integer mode. For
  5079.      registers, we can only avoid the extraction if the position is at the
  5080.      low-order bit and this is either not in the destination or we have the
  5081.      appropriate STRICT_LOW_PART operation available.
  5082.  
  5083.      For MEM, we can avoid an extract if the field starts on an appropriate
  5084.      boundary and we can change the mode of the memory reference.  However,
  5085.      we cannot directly access the MEM if we have a USE and the underlying
  5086.      MEM is not TMODE.  This combination means that MEM was being used in a
  5087.      context where bits outside its mode were being referenced; that is only
  5088.      valid in bit-field insns.  */
  5089.  
  5090.   if (tmode != BLKmode
  5091.       && ! (spans_byte && inner_mode != tmode)
  5092.       && ((pos_rtx == 0 && pos == 0 && GET_CODE (inner) != MEM
  5093.        && (! in_dest
  5094.            || (GET_CODE (inner) == REG
  5095.            && (movstrict_optab->handlers[(int) tmode].insn_code
  5096.                != CODE_FOR_nothing))))
  5097.       || (GET_CODE (inner) == MEM && pos_rtx == 0
  5098.           && (pos
  5099.           % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
  5100.              : BITS_PER_UNIT)) == 0
  5101.           /* We can't do this if we are widening INNER_MODE (it
  5102.          may not be aligned, for one thing).  */
  5103.           && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
  5104.           && (inner_mode == tmode
  5105.           || (! mode_dependent_address_p (XEXP (inner, 0))
  5106.               && ! MEM_VOLATILE_P (inner))))))
  5107.     {
  5108.       /* If INNER is a MEM, make a new MEM that encompasses just the desired
  5109.      field.  If the original and current mode are the same, we need not
  5110.      adjust the offset.  Otherwise, we do if bytes big endian.  
  5111.  
  5112.      If INNER is not a MEM, get a piece consisting of the just the field
  5113.      of interest (in this case POS must be 0).  */
  5114.  
  5115.       if (GET_CODE (inner) == MEM)
  5116.     {
  5117.       int offset;
  5118.       /* POS counts from lsb, but make OFFSET count in memory order.  */
  5119.       if (BYTES_BIG_ENDIAN)
  5120.         offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
  5121.       else
  5122.         offset = pos / BITS_PER_UNIT;
  5123.  
  5124.       new = gen_rtx (MEM, tmode, plus_constant (XEXP (inner, 0), offset));
  5125.       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
  5126.       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (inner);
  5127.       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (inner);
  5128.     }
  5129.       else if (GET_CODE (inner) == REG)
  5130.     {
  5131.       /* We can't call gen_lowpart_for_combine here since we always want
  5132.          a SUBREG and it would sometimes return a new hard register.  */
  5133.       if (tmode != inner_mode)
  5134.         new = gen_rtx (SUBREG, tmode, inner,
  5135.                (WORDS_BIG_ENDIAN
  5136.                 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD
  5137.                 ? ((GET_MODE_SIZE (inner_mode)
  5138.                 - GET_MODE_SIZE (tmode))
  5139.                    / UNITS_PER_WORD)
  5140.                 : 0));
  5141.       else
  5142.         new = inner;
  5143.     }
  5144.       else
  5145.     new = force_to_mode (inner, tmode,
  5146.                  len >= HOST_BITS_PER_WIDE_INT
  5147.                  ? GET_MODE_MASK (tmode)
  5148.                  : ((HOST_WIDE_INT) 1 << len) - 1,
  5149.                  NULL_RTX, 0);
  5150.  
  5151.       /* If this extraction is going into the destination of a SET, 
  5152.      make a STRICT_LOW_PART unless we made a MEM.  */
  5153.  
  5154.       if (in_dest)
  5155.     return (GET_CODE (new) == MEM ? new
  5156.         : (GET_CODE (new) != SUBREG
  5157.            ? gen_rtx (CLOBBER, tmode, const0_rtx)
  5158.            : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
  5159.  
  5160.       /* Otherwise, sign- or zero-extend unless we already are in the
  5161.      proper mode.  */
  5162.  
  5163.       return (mode == tmode ? new
  5164.           : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
  5165.                  mode, new));
  5166.     }
  5167.  
  5168.   /* Unless this is a COMPARE or we have a funny memory reference,
  5169.      don't do anything with zero-extending field extracts starting at
  5170.      the low-order bit since they are simple AND operations.  */
  5171.   if (pos_rtx == 0 && pos == 0 && ! in_dest
  5172.       && ! in_compare && ! spans_byte && unsignedp)
  5173.     return 0;
  5174.  
  5175.   /* Unless we are allowed to span bytes, reject this if we would be
  5176.      spanning bytes or if the position is not a constant and the length
  5177.      is not 1.  In all other cases, we would only be going outside
  5178.      out object in cases when an original shift would have been
  5179.      undefined.  */
  5180.   if (! spans_byte
  5181.       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
  5182.       || (pos_rtx != 0 && len != 1)))
  5183.     return 0;
  5184.  
  5185.   /* Get the mode to use should INNER be a MEM, the mode for the position,
  5186.      and the mode for the result.  */
  5187. #ifdef HAVE_insv
  5188.   if (in_dest)
  5189.     {
  5190.       wanted_mem_mode = insn_operand_mode[(int) CODE_FOR_insv][0];
  5191.       pos_mode = insn_operand_mode[(int) CODE_FOR_insv][2];
  5192.       extraction_mode = insn_operand_mode[(int) CODE_FOR_insv][3];
  5193.     }
  5194. #endif
  5195.  
  5196. #ifdef HAVE_extzv
  5197.   if (! in_dest && unsignedp)
  5198.     {
  5199.       wanted_mem_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
  5200.       pos_mode = insn_operand_mode[(int) CODE_FOR_extzv][3];
  5201.       extraction_mode = insn_operand_mode[(int) CODE_FOR_extzv][0];
  5202.     }
  5203. #endif
  5204.  
  5205. #ifdef HAVE_extv
  5206.   if (! in_dest && ! unsignedp)
  5207.     {
  5208.       wanted_mem_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
  5209.       pos_mode = insn_operand_mode[(int) CODE_FOR_extv][3];
  5210.       extraction_mode = insn_operand_mode[(int) CODE_FOR_extv][0];
  5211.     }
  5212. #endif
  5213.  
  5214.   /* Never narrow an object, since that might not be safe.  */
  5215.  
  5216.   if (mode != VOIDmode
  5217.       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
  5218.     extraction_mode = mode;
  5219.  
  5220.   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
  5221.       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
  5222.     pos_mode = GET_MODE (pos_rtx);
  5223.  
  5224.   /* If this is not from memory or we have to change the mode of memory and
  5225.      cannot, the desired mode is EXTRACTION_MODE.  */
  5226.   if (GET_CODE (inner) != MEM
  5227.       || (inner_mode != wanted_mem_mode
  5228.       && (mode_dependent_address_p (XEXP (inner, 0))
  5229.           || MEM_VOLATILE_P (inner))))
  5230.     wanted_mem_mode = extraction_mode;
  5231.  
  5232.   orig_pos = pos;
  5233.  
  5234.   if (BITS_BIG_ENDIAN)
  5235.     {
  5236.       /* If position is constant, compute new position.  Otherwise,
  5237.      build subtraction.  */
  5238.       if (pos_rtx == 0)
  5239.     pos = (MAX (GET_MODE_BITSIZE (is_mode),
  5240.             GET_MODE_BITSIZE (wanted_mem_mode))
  5241.            - len - pos);
  5242.       else
  5243.     pos_rtx
  5244.       = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
  5245.                  GEN_INT (MAX (GET_MODE_BITSIZE (is_mode),
  5246.                        GET_MODE_BITSIZE (wanted_mem_mode))
  5247.                       - len),
  5248.                  pos_rtx);
  5249.     }
  5250.  
  5251.   /* If INNER has a wider mode, make it smaller.  If this is a constant
  5252.      extract, try to adjust the byte to point to the byte containing
  5253.      the value.  */
  5254.   if (wanted_mem_mode != VOIDmode
  5255.       && GET_MODE_SIZE (wanted_mem_mode) < GET_MODE_SIZE (is_mode)
  5256.       && ((GET_CODE (inner) == MEM
  5257.        && (inner_mode == wanted_mem_mode
  5258.            || (! mode_dependent_address_p (XEXP (inner, 0))
  5259.            && ! MEM_VOLATILE_P (inner))))))
  5260.     {
  5261.       int offset = 0;
  5262.  
  5263.       /* The computations below will be correct if the machine is big
  5264.      endian in both bits and bytes or little endian in bits and bytes.
  5265.      If it is mixed, we must adjust.  */
  5266.          
  5267.       /* If bytes are big endian and we had a paradoxical SUBREG, we must
  5268.      adjust OFFSET to compensate. */
  5269.       if (BYTES_BIG_ENDIAN
  5270.       && ! spans_byte
  5271.       && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
  5272.     offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
  5273.  
  5274.       /* If this is a constant position, we can move to the desired byte.  */
  5275.       if (pos_rtx == 0)
  5276.     {
  5277.       offset += pos / BITS_PER_UNIT;
  5278.       pos %= GET_MODE_BITSIZE (wanted_mem_mode);
  5279.     }
  5280.  
  5281.       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
  5282.       && ! spans_byte
  5283.       && is_mode != wanted_mem_mode)
  5284.     offset = (GET_MODE_SIZE (is_mode)
  5285.           - GET_MODE_SIZE (wanted_mem_mode) - offset);
  5286.  
  5287.       if (offset != 0 || inner_mode != wanted_mem_mode)
  5288.     {
  5289.       rtx newmem = gen_rtx (MEM, wanted_mem_mode,
  5290.                 plus_constant (XEXP (inner, 0), offset));
  5291.       RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
  5292.       MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (inner);
  5293.       MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (inner);
  5294.       inner = newmem;
  5295.     }
  5296.     }
  5297.  
  5298.   /* If INNER is not memory, we can always get it into the proper mode. */
  5299.   else if (GET_CODE (inner) != MEM)
  5300.     inner = force_to_mode (inner, extraction_mode,
  5301.                pos_rtx || len + orig_pos >= HOST_BITS_PER_WIDE_INT
  5302.                ? GET_MODE_MASK (extraction_mode)
  5303.                : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
  5304.                NULL_RTX, 0);
  5305.  
  5306.   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
  5307.      have to zero extend.  Otherwise, we can just use a SUBREG.  */
  5308.   if (pos_rtx != 0
  5309.       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
  5310.     pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
  5311.   else if (pos_rtx != 0
  5312.        && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
  5313.     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
  5314.  
  5315.   /* Make POS_RTX unless we already have it and it is correct.  If we don't
  5316.      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
  5317.      be a CONST_INT. */
  5318.   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
  5319.     pos_rtx = orig_pos_rtx;
  5320.  
  5321.   else if (pos_rtx == 0)
  5322.     pos_rtx = GEN_INT (pos);
  5323.  
  5324.   /* Make the required operation.  See if we can use existing rtx.  */
  5325.   new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
  5326.              extraction_mode, inner, GEN_INT (len), pos_rtx);
  5327.   if (! in_dest)
  5328.     new = gen_lowpart_for_combine (mode, new);
  5329.  
  5330.   return new;
  5331. }
  5332.  
  5333. /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
  5334.    with any other operations in X.  Return X without that shift if so.  */
  5335.  
  5336. static rtx
  5337. extract_left_shift (x, count)
  5338.      rtx x;
  5339.      int count;
  5340. {
  5341.   enum rtx_code code = GET_CODE (x);
  5342.   enum machine_mode mode = GET_MODE (x);
  5343.   rtx tem;
  5344.  
  5345.   switch (code)
  5346.     {
  5347.     case ASHIFT:
  5348.       /* This is the shift itself.  If it is wide enough, we will return
  5349.      either the value being shifted if the shift count is equal to
  5350.      COUNT or a shift for the difference.  */
  5351.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  5352.       && INTVAL (XEXP (x, 1)) >= count)
  5353.     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
  5354.                      INTVAL (XEXP (x, 1)) - count);
  5355.       break;
  5356.  
  5357.     case NEG:  case NOT:
  5358.       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
  5359.     return gen_unary (code, mode, mode, tem);
  5360.  
  5361.       break;
  5362.  
  5363.     case PLUS:  case IOR:  case XOR:  case AND:
  5364.       /* If we can safely shift this constant and we find the inner shift,
  5365.      make a new operation.  */
  5366.       if (GET_CODE (XEXP (x,1)) == CONST_INT
  5367.       && (INTVAL (XEXP (x, 1)) & (((HOST_WIDE_INT) 1 << count)) - 1) == 0
  5368.       && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
  5369.     return gen_binary (code, mode, tem, 
  5370.                GEN_INT (INTVAL (XEXP (x, 1)) >> count));
  5371.  
  5372.       break;
  5373.     }
  5374.  
  5375.   return 0;
  5376. }
  5377.  
  5378. /* Look at the expression rooted at X.  Look for expressions
  5379.    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
  5380.    Form these expressions.
  5381.  
  5382.    Return the new rtx, usually just X.
  5383.  
  5384.    Also, for machines like the Vax that don't have logical shift insns,
  5385.    try to convert logical to arithmetic shift operations in cases where
  5386.    they are equivalent.  This undoes the canonicalizations to logical
  5387.    shifts done elsewhere.
  5388.  
  5389.    We try, as much as possible, to re-use rtl expressions to save memory.
  5390.  
  5391.    IN_CODE says what kind of expression we are processing.  Normally, it is
  5392.    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
  5393.    being kludges), it is MEM.  When processing the arguments of a comparison
  5394.    or a COMPARE against zero, it is COMPARE.  */
  5395.  
  5396. static rtx
  5397. make_compound_operation (x, in_code)
  5398.      rtx x;
  5399.      enum rtx_code in_code;
  5400. {
  5401.   enum rtx_code code = GET_CODE (x);
  5402.   enum machine_mode mode = GET_MODE (x);
  5403.   int mode_width = GET_MODE_BITSIZE (mode);
  5404.   rtx rhs, lhs;
  5405.   enum rtx_code next_code;
  5406.   int i;
  5407.   rtx new = 0;
  5408.   rtx tem;
  5409.   char *fmt;
  5410.  
  5411.   /* Select the code to be used in recursive calls.  Once we are inside an
  5412.      address, we stay there.  If we have a comparison, set to COMPARE,
  5413.      but once inside, go back to our default of SET.  */
  5414.  
  5415.   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
  5416.            : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
  5417.           && XEXP (x, 1) == const0_rtx) ? COMPARE
  5418.            : in_code == COMPARE ? SET : in_code);
  5419.  
  5420.   /* Process depending on the code of this operation.  If NEW is set
  5421.      non-zero, it will be returned.  */
  5422.  
  5423.   switch (code)
  5424.     {
  5425.     case ASHIFT:
  5426.       /* Convert shifts by constants into multiplications if inside
  5427.      an address.  */
  5428.       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
  5429.       && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
  5430.       && INTVAL (XEXP (x, 1)) >= 0)
  5431.     {
  5432.       new = make_compound_operation (XEXP (x, 0), next_code);
  5433.       new = gen_rtx_combine (MULT, mode, new,
  5434.                  GEN_INT ((HOST_WIDE_INT) 1
  5435.                       << INTVAL (XEXP (x, 1))));
  5436.     }
  5437.       break;
  5438.  
  5439.     case AND:
  5440.       /* If the second operand is not a constant, we can't do anything
  5441.      with it.  */
  5442.       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
  5443.     break;
  5444.  
  5445.       /* If the constant is a power of two minus one and the first operand
  5446.      is a logical right shift, make an extraction.  */
  5447.       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
  5448.       && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
  5449.     {
  5450.       new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
  5451.       new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
  5452.                  0, in_code == COMPARE);
  5453.     }
  5454.  
  5455.       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
  5456.       else if (GET_CODE (XEXP (x, 0)) == SUBREG
  5457.            && subreg_lowpart_p (XEXP (x, 0))
  5458.            && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
  5459.            && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
  5460.     {
  5461.       new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
  5462.                      next_code);
  5463.       new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
  5464.                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
  5465.                  0, in_code == COMPARE);
  5466.     }
  5467.       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
  5468.       else if ((GET_CODE (XEXP (x, 0)) == XOR
  5469.         || GET_CODE (XEXP (x, 0)) == IOR)
  5470.            && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
  5471.            && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
  5472.            && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
  5473.     {
  5474.       /* Apply the distributive law, and then try to make extractions.  */
  5475.       new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
  5476.                  gen_rtx (AND, mode, XEXP (XEXP (x, 0), 0),
  5477.                       XEXP (x, 1)),
  5478.                  gen_rtx (AND, mode, XEXP (XEXP (x, 0), 1),
  5479.                       XEXP (x, 1)));
  5480.       new = make_compound_operation (new, in_code);
  5481.     }
  5482.  
  5483.       /* If we are have (and (rotate X C) M) and C is larger than the number
  5484.      of bits in M, this is an extraction.  */
  5485.  
  5486.       else if (GET_CODE (XEXP (x, 0)) == ROTATE
  5487.            && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  5488.            && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
  5489.            && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
  5490.     {
  5491.       new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
  5492.       new = make_extraction (mode, new,
  5493.                  (GET_MODE_BITSIZE (mode)
  5494.                   - INTVAL (XEXP (XEXP (x, 0), 1))),
  5495.                  NULL_RTX, i, 1, 0, in_code == COMPARE);
  5496.     }
  5497.  
  5498.       /* On machines without logical shifts, if the operand of the AND is
  5499.      a logical shift and our mask turns off all the propagated sign
  5500.      bits, we can replace the logical shift with an arithmetic shift.  */
  5501.       else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
  5502.            && (lshr_optab->handlers[(int) mode].insn_code
  5503.            == CODE_FOR_nothing)
  5504.            && GET_CODE (XEXP (x, 0)) == LSHIFTRT
  5505.            && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  5506.            && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
  5507.            && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
  5508.            && mode_width <= HOST_BITS_PER_WIDE_INT)
  5509.     {
  5510.       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
  5511.  
  5512.       mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
  5513.       if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
  5514.         SUBST (XEXP (x, 0),
  5515.            gen_rtx_combine (ASHIFTRT, mode,
  5516.                     make_compound_operation (XEXP (XEXP (x, 0), 0),
  5517.                                  next_code),
  5518.                     XEXP (XEXP (x, 0), 1)));
  5519.     }
  5520.  
  5521.       /* If the constant is one less than a power of two, this might be
  5522.      representable by an extraction even if no shift is present.
  5523.      If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
  5524.      we are in a COMPARE.  */
  5525.       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
  5526.     new = make_extraction (mode,
  5527.                    make_compound_operation (XEXP (x, 0),
  5528.                             next_code),
  5529.                    0, NULL_RTX, i, 1, 0, in_code == COMPARE);
  5530.  
  5531.       /* If we are in a comparison and this is an AND with a power of two,
  5532.      convert this into the appropriate bit extract.  */
  5533.       else if (in_code == COMPARE
  5534.            && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
  5535.     new = make_extraction (mode,
  5536.                    make_compound_operation (XEXP (x, 0),
  5537.                             next_code),
  5538.                    i, NULL_RTX, 1, 1, 0, 1);
  5539.  
  5540.       break;
  5541.  
  5542.     case LSHIFTRT:
  5543.       /* If the sign bit is known to be zero, replace this with an
  5544.      arithmetic shift.  */
  5545.       if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
  5546.       && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
  5547.       && mode_width <= HOST_BITS_PER_WIDE_INT
  5548.       && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
  5549.     {
  5550.       new = gen_rtx_combine (ASHIFTRT, mode,
  5551.                  make_compound_operation (XEXP (x, 0),
  5552.                               next_code),
  5553.                  XEXP (x, 1));
  5554.       break;
  5555.     }
  5556.  
  5557.       /* ... fall through ... */
  5558.  
  5559.     case ASHIFTRT:
  5560.       lhs = XEXP (x, 0);
  5561.       rhs = XEXP (x, 1);
  5562.  
  5563.       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
  5564.      this is a SIGN_EXTRACT.  */
  5565.       if (GET_CODE (rhs) == CONST_INT
  5566.       && GET_CODE (lhs) == ASHIFT
  5567.       && GET_CODE (XEXP (lhs, 1)) == CONST_INT
  5568.       && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
  5569.     {
  5570.       new = make_compound_operation (XEXP (lhs, 0), next_code);
  5571.       new = make_extraction (mode, new,
  5572.                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
  5573.                  NULL_RTX, mode_width - INTVAL (rhs),
  5574.                  code == LSHIFTRT, 0, in_code == COMPARE);
  5575.     }
  5576.  
  5577.       /* See if we have operations between an ASHIFTRT and an ASHIFT.
  5578.      If so, try to merge the shifts into a SIGN_EXTEND.  We could
  5579.      also do this for some cases of SIGN_EXTRACT, but it doesn't
  5580.      seem worth the effort; the case checked for occurs on Alpha.  */
  5581.       
  5582.       if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
  5583.       && ! (GET_CODE (lhs) == SUBREG
  5584.         && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
  5585.       && GET_CODE (rhs) == CONST_INT
  5586.       && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
  5587.       && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
  5588.     new = make_extraction (mode, make_compound_operation (new, next_code),
  5589.                    0, NULL_RTX, mode_width - INTVAL (rhs),
  5590.                    code == LSHIFTRT, 0, in_code == COMPARE);
  5591.     
  5592.       break;
  5593.  
  5594.     case SUBREG:
  5595.       /* Call ourselves recursively on the inner expression.  If we are
  5596.      narrowing the object and it has a different RTL code from
  5597.      what it originally did, do this SUBREG as a force_to_mode.  */
  5598.  
  5599.       tem = make_compound_operation (SUBREG_REG (x), in_code);
  5600.       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
  5601.       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
  5602.       && subreg_lowpart_p (x))
  5603.     {
  5604.       rtx newer = force_to_mode (tem, mode,
  5605.                      GET_MODE_MASK (mode), NULL_RTX, 0);
  5606.  
  5607.       /* If we have something other than a SUBREG, we might have
  5608.          done an expansion, so rerun outselves.  */
  5609.       if (GET_CODE (newer) != SUBREG)
  5610.         newer = make_compound_operation (newer, in_code);
  5611.  
  5612.       return newer;
  5613.     }
  5614.     }
  5615.  
  5616.   if (new)
  5617.     {
  5618.       x = gen_lowpart_for_combine (mode, new);
  5619.       code = GET_CODE (x);
  5620.     }
  5621.  
  5622.   /* Now recursively process each operand of this operation.  */
  5623.   fmt = GET_RTX_FORMAT (code);
  5624.   for (i = 0; i < GET_RTX_LENGTH (code); i++)
  5625.     if (fmt[i] == 'e')
  5626.       {
  5627.     new = make_compound_operation (XEXP (x, i), next_code);
  5628.     SUBST (XEXP (x, i), new);
  5629.       }
  5630.  
  5631.   return x;
  5632. }
  5633.  
  5634. /* Given M see if it is a value that would select a field of bits
  5635.     within an item, but not the entire word.  Return -1 if not.
  5636.     Otherwise, return the starting position of the field, where 0 is the
  5637.     low-order bit.
  5638.  
  5639.    *PLEN is set to the length of the field.  */
  5640.  
  5641. static int
  5642. get_pos_from_mask (m, plen)
  5643.      unsigned HOST_WIDE_INT m;
  5644.      int *plen;
  5645. {
  5646.   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
  5647.   int pos = exact_log2 (m & - m);
  5648.  
  5649.   if (pos < 0)
  5650.     return -1;
  5651.  
  5652.   /* Now shift off the low-order zero bits and see if we have a power of
  5653.      two minus 1.  */
  5654.   *plen = exact_log2 ((m >> pos) + 1);
  5655.  
  5656.   if (*plen <= 0)
  5657.     return -1;
  5658.  
  5659.   return pos;
  5660. }
  5661.  
  5662. /* See if X can be simplified knowing that we will only refer to it in
  5663.    MODE and will only refer to those bits that are nonzero in MASK.
  5664.    If other bits are being computed or if masking operations are done
  5665.    that select a superset of the bits in MASK, they can sometimes be
  5666.    ignored.
  5667.  
  5668.    Return a possibly simplified expression, but always convert X to
  5669.    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
  5670.  
  5671.    Also, if REG is non-zero and X is a register equal in value to REG, 
  5672.    replace X with REG.
  5673.  
  5674.    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
  5675.    are all off in X.  This is used when X will be complemented, by either
  5676.    NOT, NEG, or XOR.  */
  5677.  
  5678. static rtx
  5679. force_to_mode (x, mode, mask, reg, just_select)
  5680.      rtx x;
  5681.      enum machine_mode mode;
  5682.      unsigned HOST_WIDE_INT mask;
  5683.      rtx reg;
  5684.      int just_select;
  5685. {
  5686.   enum rtx_code code = GET_CODE (x);
  5687.   int next_select = just_select || code == XOR || code == NOT || code == NEG;
  5688.   enum machine_mode op_mode;
  5689.   unsigned HOST_WIDE_INT fuller_mask, nonzero;
  5690.   rtx op0, op1, temp;
  5691.  
  5692.   /* If this is a CALL, don't do anything.  Some of the code below
  5693.      will do the wrong thing since the mode of a CALL is VOIDmode.  */
  5694.   if (code == CALL)
  5695.     return x;
  5696.  
  5697.   /* We want to perform the operation is its present mode unless we know
  5698.      that the operation is valid in MODE, in which case we do the operation
  5699.      in MODE.  */
  5700.   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
  5701.           && code_to_optab[(int) code] != 0
  5702.           && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
  5703.           != CODE_FOR_nothing))
  5704.          ? mode : GET_MODE (x));
  5705.  
  5706.   /* It is not valid to do a right-shift in a narrower mode
  5707.      than the one it came in with.  */
  5708.   if ((code == LSHIFTRT || code == ASHIFTRT)
  5709.       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
  5710.     op_mode = GET_MODE (x);
  5711.  
  5712.   /* Truncate MASK to fit OP_MODE.  */
  5713.   if (op_mode)
  5714.     mask &= GET_MODE_MASK (op_mode);
  5715.  
  5716.   /* When we have an arithmetic operation, or a shift whose count we
  5717.      do not know, we need to assume that all bit the up to the highest-order
  5718.      bit in MASK will be needed.  This is how we form such a mask.  */
  5719.   if (op_mode)
  5720.     fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
  5721.            ? GET_MODE_MASK (op_mode)
  5722.            : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
  5723.   else
  5724.     fuller_mask = ~ (HOST_WIDE_INT) 0;
  5725.  
  5726.   /* Determine what bits of X are guaranteed to be (non)zero.  */
  5727.   nonzero = nonzero_bits (x, mode);
  5728.  
  5729.   /* If none of the bits in X are needed, return a zero.  */
  5730.   if (! just_select && (nonzero & mask) == 0)
  5731.     return const0_rtx;
  5732.  
  5733.   /* If X is a CONST_INT, return a new one.  Do this here since the
  5734.      test below will fail.  */
  5735.   if (GET_CODE (x) == CONST_INT)
  5736.     {
  5737.       HOST_WIDE_INT cval = INTVAL (x) & mask;
  5738.       int width = GET_MODE_BITSIZE (mode);
  5739.  
  5740.       /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
  5741.      number, sign extend it.  */
  5742.       if (width > 0 && width < HOST_BITS_PER_WIDE_INT
  5743.       && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
  5744.     cval |= (HOST_WIDE_INT) -1 << width;
  5745.     
  5746.       return GEN_INT (cval);
  5747.     }
  5748.  
  5749.   /* If X is narrower than MODE and we want all the bits in X's mode, just
  5750.      get X in the proper mode.  */
  5751.   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
  5752.       && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
  5753.     return gen_lowpart_for_combine (mode, x);
  5754.  
  5755.   /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
  5756.      MASK are already known to be zero in X, we need not do anything.  */
  5757.   if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
  5758.     return x;
  5759.  
  5760.   switch (code)
  5761.     {
  5762.     case CLOBBER:
  5763.       /* If X is a (clobber (const_int)), return it since we know we are
  5764.      generating something that won't match. */
  5765.       return x;
  5766.  
  5767.     case USE:
  5768.       /* X is a (use (mem ..)) that was made from a bit-field extraction that
  5769.      spanned the boundary of the MEM.  If we are now masking so it is
  5770.      within that boundary, we don't need the USE any more.  */
  5771.       if (! BITS_BIG_ENDIAN
  5772.       && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
  5773.     return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
  5774.       break;
  5775.  
  5776.     case SIGN_EXTEND:
  5777.     case ZERO_EXTEND:
  5778.     case ZERO_EXTRACT:
  5779.     case SIGN_EXTRACT:
  5780.       x = expand_compound_operation (x);
  5781.       if (GET_CODE (x) != code)
  5782.     return force_to_mode (x, mode, mask, reg, next_select);
  5783.       break;
  5784.  
  5785.     case REG:
  5786.       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
  5787.                || rtx_equal_p (reg, get_last_value (x))))
  5788.     x = reg;
  5789.       break;
  5790.  
  5791.     case SUBREG:
  5792.       if (subreg_lowpart_p (x)
  5793.       /* We can ignore the effect of this SUBREG if it narrows the mode or
  5794.          if the constant masks to zero all the bits the mode doesn't
  5795.          have.  */
  5796.       && ((GET_MODE_SIZE (GET_MODE (x))
  5797.            < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
  5798.           || (0 == (mask
  5799.             & GET_MODE_MASK (GET_MODE (x))
  5800.             & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
  5801.     return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
  5802.       break;
  5803.  
  5804.     case AND:
  5805.       /* If this is an AND with a constant, convert it into an AND
  5806.      whose constant is the AND of that constant with MASK.  If it
  5807.      remains an AND of MASK, delete it since it is redundant.  */
  5808.  
  5809.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  5810.     {
  5811.       x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
  5812.                       mask & INTVAL (XEXP (x, 1)));
  5813.  
  5814.       /* If X is still an AND, see if it is an AND with a mask that
  5815.          is just some low-order bits.  If so, and it is MASK, we don't
  5816.          need it.  */
  5817.  
  5818.       if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
  5819.           && INTVAL (XEXP (x, 1)) == mask)
  5820.         x = XEXP (x, 0);
  5821.  
  5822.       /* If it remains an AND, try making another AND with the bits
  5823.          in the mode mask that aren't in MASK turned on.  If the
  5824.          constant in the AND is wide enough, this might make a
  5825.          cheaper constant.  */
  5826.  
  5827.       if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
  5828.           && GET_MODE_MASK (GET_MODE (x)) != mask
  5829.           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
  5830.         {
  5831.           HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
  5832.                     | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
  5833.           int width = GET_MODE_BITSIZE (GET_MODE (x));
  5834.           rtx y;
  5835.  
  5836.           /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
  5837.          number, sign extend it.  */
  5838.           if (width > 0 && width < HOST_BITS_PER_WIDE_INT
  5839.           && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
  5840.         cval |= (HOST_WIDE_INT) -1 << width;
  5841.  
  5842.           y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
  5843.           if (rtx_cost (y, SET) < rtx_cost (x, SET))
  5844.         x = y;
  5845.         }
  5846.  
  5847.       break;
  5848.     }
  5849.  
  5850.       goto binop;
  5851.  
  5852.     case PLUS:
  5853.       /* In (and (plus FOO C1) M), if M is a mask that just turns off
  5854.      low-order bits (as in an alignment operation) and FOO is already
  5855.      aligned to that boundary, mask C1 to that boundary as well.
  5856.      This may eliminate that PLUS and, later, the AND.  */
  5857.  
  5858.       {
  5859.     int width = GET_MODE_BITSIZE (mode);
  5860.     unsigned HOST_WIDE_INT smask = mask;
  5861.  
  5862.     /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
  5863.        number, sign extend it.  */
  5864.  
  5865.     if (width < HOST_BITS_PER_WIDE_INT
  5866.         && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
  5867.       smask |= (HOST_WIDE_INT) -1 << width;
  5868.  
  5869.     if (GET_CODE (XEXP (x, 1)) == CONST_INT
  5870.         && exact_log2 (- smask) >= 0
  5871.         && (nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0
  5872.         && (INTVAL (XEXP (x, 1)) & ~ mask) != 0)
  5873.       return force_to_mode (plus_constant (XEXP (x, 0),
  5874.                            INTVAL (XEXP (x, 1)) & mask),
  5875.                 mode, mask, reg, next_select);
  5876.       }
  5877.  
  5878.       /* ... fall through ... */
  5879.  
  5880.     case MINUS:
  5881.     case MULT:
  5882.       /* For PLUS, MINUS and MULT, we need any bits less significant than the
  5883.      most significant bit in MASK since carries from those bits will
  5884.      affect the bits we are interested in.  */
  5885.       mask = fuller_mask;
  5886.       goto binop;
  5887.  
  5888.     case IOR:
  5889.     case XOR:
  5890.       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
  5891.      LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
  5892.      operation which may be a bitfield extraction.  Ensure that the
  5893.      constant we form is not wider than the mode of X.  */
  5894.  
  5895.       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
  5896.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  5897.       && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
  5898.       && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
  5899.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  5900.       && ((INTVAL (XEXP (XEXP (x, 0), 1))
  5901.            + floor_log2 (INTVAL (XEXP (x, 1))))
  5902.           < GET_MODE_BITSIZE (GET_MODE (x)))
  5903.       && (INTVAL (XEXP (x, 1))
  5904.           & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x)) == 0))
  5905.     {
  5906.       temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
  5907.                   << INTVAL (XEXP (XEXP (x, 0), 1)));
  5908.       temp = gen_binary (GET_CODE (x), GET_MODE (x),
  5909.                  XEXP (XEXP (x, 0), 0), temp);
  5910.       x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (x, 1));
  5911.       return force_to_mode (x, mode, mask, reg, next_select);
  5912.     }
  5913.  
  5914.     binop:
  5915.       /* For most binary operations, just propagate into the operation and
  5916.      change the mode if we have an operation of that mode.   */
  5917.  
  5918.       op0 = gen_lowpart_for_combine (op_mode,
  5919.                      force_to_mode (XEXP (x, 0), mode, mask,
  5920.                             reg, next_select));
  5921.       op1 = gen_lowpart_for_combine (op_mode,
  5922.                      force_to_mode (XEXP (x, 1), mode, mask,
  5923.                             reg, next_select));
  5924.  
  5925.       /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
  5926.      MASK since OP1 might have been sign-extended but we never want
  5927.      to turn on extra bits, since combine might have previously relied
  5928.      on them being off.  */
  5929.       if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
  5930.       && (INTVAL (op1) & mask) != 0)
  5931.     op1 = GEN_INT (INTVAL (op1) & mask);
  5932.      
  5933.       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  5934.     x = gen_binary (code, op_mode, op0, op1);
  5935.       break;
  5936.  
  5937.     case ASHIFT:
  5938.       /* For left shifts, do the same, but just for the first operand.
  5939.      However, we cannot do anything with shifts where we cannot
  5940.      guarantee that the counts are smaller than the size of the mode
  5941.      because such a count will have a different meaning in a
  5942.      wider mode.  */
  5943.  
  5944.       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
  5945.          && INTVAL (XEXP (x, 1)) >= 0
  5946.          && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
  5947.       && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
  5948.         && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
  5949.             < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
  5950.     break;
  5951.     
  5952.       /* If the shift count is a constant and we can do arithmetic in
  5953.      the mode of the shift, refine which bits we need.  Otherwise, use the
  5954.      conservative form of the mask.  */
  5955.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  5956.       && INTVAL (XEXP (x, 1)) >= 0
  5957.       && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
  5958.       && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
  5959.     mask >>= INTVAL (XEXP (x, 1));
  5960.       else
  5961.     mask = fuller_mask;
  5962.  
  5963.       op0 = gen_lowpart_for_combine (op_mode,
  5964.                      force_to_mode (XEXP (x, 0), op_mode,
  5965.                             mask, reg, next_select));
  5966.  
  5967.       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
  5968.     x =  gen_binary (code, op_mode, op0, XEXP (x, 1));
  5969.       break;
  5970.  
  5971.     case LSHIFTRT:
  5972.       /* Here we can only do something if the shift count is a constant,
  5973.      this shift constant is valid for the host, and we can do arithmetic
  5974.      in OP_MODE.  */
  5975.  
  5976.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  5977.       && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
  5978.       && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
  5979.     {
  5980.       rtx inner = XEXP (x, 0);
  5981.  
  5982.       /* Select the mask of the bits we need for the shift operand.  */
  5983.       mask <<= INTVAL (XEXP (x, 1));
  5984.  
  5985.       /* We can only change the mode of the shift if we can do arithmetic
  5986.          in the mode of the shift and MASK is no wider than the width of
  5987.          OP_MODE.  */
  5988.       if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
  5989.           || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
  5990.         op_mode = GET_MODE (x);
  5991.  
  5992.       inner = force_to_mode (inner, op_mode, mask, reg, next_select);
  5993.  
  5994.       if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
  5995.         x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
  5996.     }
  5997.  
  5998.       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
  5999.      shift and AND produces only copies of the sign bit (C2 is one less
  6000.      than a power of two), we can do this with just a shift.  */
  6001.  
  6002.       if (GET_CODE (x) == LSHIFTRT
  6003.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  6004.       && ((INTVAL (XEXP (x, 1))
  6005.            + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
  6006.           >= GET_MODE_BITSIZE (GET_MODE (x)))
  6007.       && exact_log2 (mask + 1) >= 0
  6008.       && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
  6009.           >= exact_log2 (mask + 1)))
  6010.     x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
  6011.             GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
  6012.                  - exact_log2 (mask + 1)));
  6013.       break;
  6014.  
  6015.     case ASHIFTRT:
  6016.       /* If we are just looking for the sign bit, we don't need this shift at
  6017.      all, even if it has a variable count.  */
  6018.       if (mask == ((HOST_WIDE_INT) 1
  6019.            << (GET_MODE_BITSIZE (GET_MODE (x)) - 1)))
  6020.     return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
  6021.  
  6022.       /* If this is a shift by a constant, get a mask that contains those bits
  6023.      that are not copies of the sign bit.  We then have two cases:  If
  6024.      MASK only includes those bits, this can be a logical shift, which may
  6025.      allow simplifications.  If MASK is a single-bit field not within
  6026.      those bits, we are requesting a copy of the sign bit and hence can
  6027.      shift the sign bit to the appropriate location.  */
  6028.  
  6029.       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
  6030.       && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
  6031.     {
  6032.       int i = -1;
  6033.  
  6034.       nonzero = GET_MODE_MASK (GET_MODE (x));
  6035.       nonzero >>= INTVAL (XEXP (x, 1));
  6036.  
  6037.       if ((mask & ~ nonzero) == 0
  6038.           || (i = exact_log2 (mask)) >= 0)
  6039.         {
  6040.           x = simplify_shift_const
  6041.         (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
  6042.          i < 0 ? INTVAL (XEXP (x, 1))
  6043.          : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
  6044.  
  6045.           if (GET_CODE (x) != ASHIFTRT)
  6046.         return force_to_mode (x, mode, mask, reg, next_select);
  6047.         }
  6048.     }
  6049.  
  6050.       /* If MASK is 1, convert this to a LSHIFTRT.  This can be done
  6051.      even if the shift count isn't a constant.  */
  6052.       if (mask == 1)
  6053.     x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
  6054.  
  6055.       /* If this is a sign-extension operation that just affects bits
  6056.      we don't care about, remove it.  Be sure the call above returned
  6057.      something that is still a shift.  */
  6058.  
  6059.       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
  6060.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  6061.       && INTVAL (XEXP (x, 1)) >= 0
  6062.       && (INTVAL (XEXP (x, 1))
  6063.           <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
  6064.       && GET_CODE (XEXP (x, 0)) == ASHIFT
  6065.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  6066.       && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
  6067.     return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
  6068.                   reg, next_select);
  6069.  
  6070.       break;
  6071.  
  6072.     case ROTATE:
  6073.     case ROTATERT:
  6074.       /* If the shift count is constant and we can do computations
  6075.      in the mode of X, compute where the bits we care about are.
  6076.      Otherwise, we can't do anything.  Don't change the mode of
  6077.      the shift or propagate MODE into the shift, though.  */
  6078.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  6079.       && INTVAL (XEXP (x, 1)) >= 0)
  6080.     {
  6081.       temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
  6082.                         GET_MODE (x), GEN_INT (mask),
  6083.                         XEXP (x, 1));
  6084.       if (temp && GET_CODE(temp) == CONST_INT)
  6085.         SUBST (XEXP (x, 0),
  6086.            force_to_mode (XEXP (x, 0), GET_MODE (x),
  6087.                   INTVAL (temp), reg, next_select));
  6088.     }
  6089.       break;
  6090.     
  6091.     case NEG:
  6092.       /* If we just want the low-order bit, the NEG isn't needed since it
  6093.      won't change the low-order bit.    */
  6094.       if (mask == 1)
  6095.     return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
  6096.  
  6097.       /* We need any bits less significant than the most significant bit in
  6098.      MASK since carries from those bits will affect the bits we are
  6099.      interested in.  */
  6100.       mask = fuller_mask;
  6101.       goto unop;
  6102.  
  6103.     case NOT:
  6104.       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
  6105.      same as the XOR case above.  Ensure that the constant we form is not
  6106.      wider than the mode of X.  */
  6107.  
  6108.       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
  6109.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  6110.       && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
  6111.       && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
  6112.           < GET_MODE_BITSIZE (GET_MODE (x)))
  6113.       && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
  6114.     {
  6115.       temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
  6116.       temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
  6117.       x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
  6118.  
  6119.       return force_to_mode (x, mode, mask, reg, next_select);
  6120.     }
  6121.  
  6122.     unop:
  6123.       op0 = gen_lowpart_for_combine (op_mode,
  6124.                      force_to_mode (XEXP (x, 0), mode, mask,
  6125.                             reg, next_select));
  6126.       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
  6127.     x = gen_unary (code, op_mode, op_mode, op0);
  6128.       break;
  6129.  
  6130.     case NE:
  6131.       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
  6132.      in STORE_FLAG_VALUE and FOO has no bits that might be nonzero not
  6133.      in CONST.  */
  6134.       if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 0) == const0_rtx
  6135.       && (nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0)
  6136.     return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
  6137.  
  6138.       break;
  6139.  
  6140.     case IF_THEN_ELSE:
  6141.       /* We have no way of knowing if the IF_THEN_ELSE can itself be
  6142.      written in a narrower mode.  We play it safe and do not do so.  */
  6143.  
  6144.       SUBST (XEXP (x, 1),
  6145.          gen_lowpart_for_combine (GET_MODE (x),
  6146.                       force_to_mode (XEXP (x, 1), mode,
  6147.                              mask, reg, next_select)));
  6148.       SUBST (XEXP (x, 2),
  6149.          gen_lowpart_for_combine (GET_MODE (x),
  6150.                       force_to_mode (XEXP (x, 2), mode,
  6151.                              mask, reg,next_select)));
  6152.       break;
  6153.     }
  6154.  
  6155.   /* Ensure we return a value of the proper mode.  */
  6156.   return gen_lowpart_for_combine (mode, x);
  6157. }
  6158.  
  6159. /* Return nonzero if X is an expression that has one of two values depending on
  6160.    whether some other value is zero or nonzero.  In that case, we return the
  6161.    value that is being tested, *PTRUE is set to the value if the rtx being
  6162.    returned has a nonzero value, and *PFALSE is set to the other alternative.
  6163.  
  6164.    If we return zero, we set *PTRUE and *PFALSE to X.  */
  6165.  
  6166. static rtx
  6167. if_then_else_cond (x, ptrue, pfalse)
  6168.      rtx x;
  6169.      rtx *ptrue, *pfalse;
  6170. {
  6171.   enum machine_mode mode = GET_MODE (x);
  6172.   enum rtx_code code = GET_CODE (x);
  6173.   int size = GET_MODE_BITSIZE (mode);
  6174.   rtx cond0, cond1, true0, true1, false0, false1;
  6175.   unsigned HOST_WIDE_INT nz;
  6176.  
  6177.   /* If this is a unary operation whose operand has one of two values, apply
  6178.      our opcode to compute those values.  */
  6179.   if (GET_RTX_CLASS (code) == '1'
  6180.       && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
  6181.     {
  6182.       *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
  6183.       *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
  6184.       return cond0;
  6185.     }
  6186.  
  6187.   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
  6188.      make can't possibly match and would suppress other optimizations.  */
  6189.   else if (code == COMPARE)
  6190.     ;
  6191.  
  6192.   /* If this is a binary operation, see if either side has only one of two
  6193.      values.  If either one does or if both do and they are conditional on
  6194.      the same value, compute the new true and false values.  */
  6195.   else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
  6196.        || GET_RTX_CLASS (code) == '<')
  6197.     {
  6198.       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
  6199.       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
  6200.  
  6201.       if ((cond0 != 0 || cond1 != 0)
  6202.       && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
  6203.     {
  6204.       *ptrue = gen_binary (code, mode, true0, true1);
  6205.       *pfalse = gen_binary (code, mode, false0, false1);
  6206.       return cond0 ? cond0 : cond1;
  6207.     }
  6208.  
  6209. #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
  6210.  
  6211.       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
  6212.      operands is zero when the other is non-zero, and vice-versa.  */
  6213.  
  6214.       if ((code == PLUS || code == IOR || code == XOR || code == MINUS
  6215.        || code == UMAX)
  6216.       && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
  6217.     {
  6218.       rtx op0 = XEXP (XEXP (x, 0), 1);
  6219.       rtx op1 = XEXP (XEXP (x, 1), 1);
  6220.  
  6221.       cond0 = XEXP (XEXP (x, 0), 0);
  6222.       cond1 = XEXP (XEXP (x, 1), 0);
  6223.  
  6224.       if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
  6225.           && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
  6226.           && reversible_comparison_p (cond1)
  6227.           && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
  6228.            && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
  6229.            && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
  6230.           || ((swap_condition (GET_CODE (cond0))
  6231.                == reverse_condition (GET_CODE (cond1)))
  6232.               && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
  6233.               && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
  6234.           && ! side_effects_p (x))
  6235.         {
  6236.           *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
  6237.           *pfalse = gen_binary (MULT, mode, 
  6238.                     (code == MINUS 
  6239.                      ? gen_unary (NEG, mode, mode, op1) : op1),
  6240.                     const_true_rtx);
  6241.           return cond0;
  6242.         }
  6243.     }
  6244.  
  6245.       /* Similarly for MULT, AND and UMIN, execpt that for these the result
  6246.      is always zero.  */
  6247.       if ((code == MULT || code == AND || code == UMIN)
  6248.       && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
  6249.     {
  6250.       cond0 = XEXP (XEXP (x, 0), 0);
  6251.       cond1 = XEXP (XEXP (x, 1), 0);
  6252.  
  6253.       if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
  6254.           && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
  6255.           && reversible_comparison_p (cond1)
  6256.           && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
  6257.            && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
  6258.            && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
  6259.           || ((swap_condition (GET_CODE (cond0))
  6260.                == reverse_condition (GET_CODE (cond1)))
  6261.               && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
  6262.               && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
  6263.           && ! side_effects_p (x))
  6264.         {
  6265.           *ptrue = *pfalse = const0_rtx;
  6266.           return cond0;
  6267.         }
  6268.     }
  6269. #endif
  6270.     }
  6271.  
  6272.   else if (code == IF_THEN_ELSE)
  6273.     {
  6274.       /* If we have IF_THEN_ELSE already, extract the condition and
  6275.      canonicalize it if it is NE or EQ.  */
  6276.       cond0 = XEXP (x, 0);
  6277.       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
  6278.       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
  6279.     return XEXP (cond0, 0);
  6280.       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
  6281.     {
  6282.       *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
  6283.       return XEXP (cond0, 0);
  6284.     }
  6285.       else
  6286.     return cond0;
  6287.     }
  6288.  
  6289.   /* If X is a normal SUBREG with both inner and outer modes integral,
  6290.      we can narrow both the true and false values of the inner expression,
  6291.      if there is a condition.  */
  6292.   else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
  6293.        && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
  6294.        && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
  6295.        && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
  6296.                            &true0, &false0)))
  6297.     {
  6298.       *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
  6299.       *pfalse
  6300.     = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
  6301.  
  6302.       return cond0;
  6303.     }
  6304.  
  6305.   /* If X is a constant, this isn't special and will cause confusions
  6306.      if we treat it as such.  Likewise if it is equivalent to a constant.  */
  6307.   else if (CONSTANT_P (x)
  6308.        || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
  6309.     ;
  6310.  
  6311.   /* If X is known to be either 0 or -1, those are the true and 
  6312.      false values when testing X.  */
  6313.   else if (num_sign_bit_copies (x, mode) == size)
  6314.     {
  6315.       *ptrue = constm1_rtx, *pfalse = const0_rtx;
  6316.       return x;
  6317.     }
  6318.  
  6319.   /* Likewise for 0 or a single bit.  */
  6320.   else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
  6321.     {
  6322.       *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
  6323.       return x;
  6324.     }
  6325.  
  6326.   /* Otherwise fail; show no condition with true and false values the same.  */
  6327.   *ptrue = *pfalse = x;
  6328.   return 0;
  6329. }
  6330.  
  6331. /* Return the value of expression X given the fact that condition COND
  6332.    is known to be true when applied to REG as its first operand and VAL
  6333.    as its second.  X is known to not be shared and so can be modified in
  6334.    place.
  6335.  
  6336.    We only handle the simplest cases, and specifically those cases that
  6337.    arise with IF_THEN_ELSE expressions.  */
  6338.  
  6339. static rtx
  6340. known_cond (x, cond, reg, val)
  6341.      rtx x;
  6342.      enum rtx_code cond;
  6343.      rtx reg, val;
  6344. {
  6345.   enum rtx_code code = GET_CODE (x);
  6346.   rtx temp;
  6347.   char *fmt;
  6348.   int i, j;
  6349.  
  6350.   if (side_effects_p (x))
  6351.     return x;
  6352.  
  6353.   if (cond == EQ && rtx_equal_p (x, reg))
  6354.     return val;
  6355.  
  6356.   /* If X is (abs REG) and we know something about REG's relationship
  6357.      with zero, we may be able to simplify this.  */
  6358.  
  6359.   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
  6360.     switch (cond)
  6361.       {
  6362.       case GE:  case GT:  case EQ:
  6363.     return XEXP (x, 0);
  6364.       case LT:  case LE:
  6365.     return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
  6366.               XEXP (x, 0));
  6367.       }
  6368.  
  6369.   /* The only other cases we handle are MIN, MAX, and comparisons if the
  6370.      operands are the same as REG and VAL.  */
  6371.  
  6372.   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
  6373.     {
  6374.       if (rtx_equal_p (XEXP (x, 0), val))
  6375.     cond = swap_condition (cond), temp = val, val = reg, reg = temp;
  6376.  
  6377.       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
  6378.     {
  6379.       if (GET_RTX_CLASS (code) == '<')
  6380.         return (comparison_dominates_p (cond, code) ? const_true_rtx
  6381.             : (comparison_dominates_p (cond,
  6382.                            reverse_condition (code))
  6383.                ? const0_rtx : x));
  6384.  
  6385.       else if (code == SMAX || code == SMIN
  6386.            || code == UMIN || code == UMAX)
  6387.         {
  6388.           int unsignedp = (code == UMIN || code == UMAX);
  6389.  
  6390.           if (code == SMAX || code == UMAX)
  6391.         cond = reverse_condition (cond);
  6392.  
  6393.           switch (cond)
  6394.         {
  6395.         case GE:   case GT:
  6396.           return unsignedp ? x : XEXP (x, 1);
  6397.         case LE:   case LT:
  6398.           return unsignedp ? x : XEXP (x, 0);
  6399.         case GEU:  case GTU:
  6400.           return unsignedp ? XEXP (x, 1) : x;
  6401.         case LEU:  case LTU:
  6402.           return unsignedp ? XEXP (x, 0) : x;
  6403.         }
  6404.         }
  6405.     }
  6406.     }
  6407.  
  6408.   fmt = GET_RTX_FORMAT (code);
  6409.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  6410.     {
  6411.       if (fmt[i] == 'e')
  6412.     SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
  6413.       else if (fmt[i] == 'E')
  6414.     for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  6415.       SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
  6416.                         cond, reg, val));
  6417.     }
  6418.  
  6419.   return x;
  6420. }
  6421.  
  6422. /* See if X, a SET operation, can be rewritten as a bit-field assignment.
  6423.    Return that assignment if so.
  6424.  
  6425.    We only handle the most common cases.  */
  6426.  
  6427. static rtx
  6428. make_field_assignment (x)
  6429.      rtx x;
  6430. {
  6431.   rtx dest = SET_DEST (x);
  6432.   rtx src = SET_SRC (x);
  6433.   rtx assign;
  6434.   HOST_WIDE_INT c1;
  6435.   int pos, len;
  6436.   rtx other;
  6437.   enum machine_mode mode;
  6438.  
  6439.   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
  6440.      a clear of a one-bit field.  We will have changed it to
  6441.      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
  6442.      for a SUBREG.  */
  6443.  
  6444.   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
  6445.       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
  6446.       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
  6447.       && (rtx_equal_p (dest, XEXP (src, 1))
  6448.       || rtx_equal_p (dest, get_last_value (XEXP (src, 1)))
  6449.       || rtx_equal_p (get_last_value (dest), XEXP (src, 1))))
  6450.     {
  6451.       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
  6452.                 1, 1, 1, 0);
  6453.       return gen_rtx (SET, VOIDmode, assign, const0_rtx);
  6454.     }
  6455.  
  6456.   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
  6457.        && subreg_lowpart_p (XEXP (src, 0))
  6458.        && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0))) 
  6459.            < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
  6460.        && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
  6461.        && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
  6462.        && (rtx_equal_p (dest, XEXP (src, 1))
  6463.            || rtx_equal_p (dest, get_last_value (XEXP (src, 1)))
  6464.            || rtx_equal_p (get_last_value (dest), XEXP (src, 1))))
  6465.     {
  6466.       assign = make_extraction (VOIDmode, dest, 0,
  6467.                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
  6468.                 1, 1, 1, 0);
  6469.       return gen_rtx (SET, VOIDmode, assign, const0_rtx);
  6470.     }
  6471.  
  6472.   /* If SRC is (ior (ashift (const_int 1) POS DEST)), this is a set of a
  6473.      one-bit field.  */
  6474.   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
  6475.        && XEXP (XEXP (src, 0), 0) == const1_rtx
  6476.        && (rtx_equal_p (dest, XEXP (src, 1))
  6477.            || rtx_equal_p (dest, get_last_value (XEXP (src, 1)))
  6478.            || rtx_equal_p (get_last_value (dest), XEXP (src, 1))))
  6479.     {
  6480.       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
  6481.                 1, 1, 1, 0);
  6482.       return gen_rtx (SET, VOIDmode, assign, const1_rtx);
  6483.     }
  6484.  
  6485.   /* The other case we handle is assignments into a constant-position
  6486.      field.  They look like (ior (and DEST C1) OTHER).  If C1 represents
  6487.      a mask that has all one bits except for a group of zero bits and
  6488.      OTHER is known to have zeros where C1 has ones, this is such an
  6489.      assignment.  Compute the position and length from C1.  Shift OTHER
  6490.      to the appropriate position, force it to the required mode, and
  6491.      make the extraction.  Check for the AND in both operands.  */
  6492.  
  6493.   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == AND
  6494.       && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT
  6495.       && (rtx_equal_p (XEXP (XEXP (src, 0), 0), dest)
  6496.       || rtx_equal_p (XEXP (XEXP (src, 0), 0), get_last_value (dest))
  6497.       || rtx_equal_p (get_last_value (XEXP (XEXP (src, 0), 1)), dest)))
  6498.     c1 = INTVAL (XEXP (XEXP (src, 0), 1)), other = XEXP (src, 1);
  6499.   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 1)) == AND
  6500.        && GET_CODE (XEXP (XEXP (src, 1), 1)) == CONST_INT
  6501.        && (rtx_equal_p (XEXP (XEXP (src, 1), 0), dest)
  6502.            || rtx_equal_p (XEXP (XEXP (src, 1), 0), get_last_value (dest))
  6503.            || rtx_equal_p (get_last_value (XEXP (XEXP (src, 1), 0)),
  6504.                    dest)))
  6505.     c1 = INTVAL (XEXP (XEXP (src, 1), 1)), other = XEXP (src, 0);
  6506.   else
  6507.     return x;
  6508.  
  6509.   pos = get_pos_from_mask (c1 ^ GET_MODE_MASK (GET_MODE (dest)), &len);
  6510.   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
  6511.       || (GET_MODE_BITSIZE (GET_MODE (other)) <= HOST_BITS_PER_WIDE_INT
  6512.       && (c1 & nonzero_bits (other, GET_MODE (other))) != 0))
  6513.     return x;
  6514.  
  6515.   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
  6516.  
  6517.   /* The mode to use for the source is the mode of the assignment, or of
  6518.      what is inside a possible STRICT_LOW_PART.  */
  6519.   mode = (GET_CODE (assign) == STRICT_LOW_PART 
  6520.       ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
  6521.  
  6522.   /* Shift OTHER right POS places and make it the source, restricting it
  6523.      to the proper length and mode.  */
  6524.  
  6525.   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
  6526.                          GET_MODE (src), other, pos),
  6527.                mode,
  6528.                GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
  6529.                ? GET_MODE_MASK (mode)
  6530.                : ((HOST_WIDE_INT) 1 << len) - 1,
  6531.                dest, 0);
  6532.  
  6533.   return gen_rtx_combine (SET, VOIDmode, assign, src);
  6534. }
  6535.  
  6536. /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
  6537.    if so.  */
  6538.  
  6539. static rtx
  6540. apply_distributive_law (x)
  6541.      rtx x;
  6542. {
  6543.   enum rtx_code code = GET_CODE (x);
  6544.   rtx lhs, rhs, other;
  6545.   rtx tem;
  6546.   enum rtx_code inner_code;
  6547.  
  6548.   /* Distributivity is not true for floating point.
  6549.      It can change the value.  So don't do it.
  6550.      -- rms and moshier@world.std.com.  */
  6551.   if (FLOAT_MODE_P (GET_MODE (x)))
  6552.     return x;
  6553.  
  6554.   /* The outer operation can only be one of the following:  */
  6555.   if (code != IOR && code != AND && code != XOR
  6556.       && code != PLUS && code != MINUS)
  6557.     return x;
  6558.  
  6559.   lhs = XEXP (x, 0), rhs = XEXP (x, 1);
  6560.  
  6561.   /* If either operand is a primitive we can't do anything, so get out fast. */
  6562.   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
  6563.       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
  6564.     return x;
  6565.  
  6566.   lhs = expand_compound_operation (lhs);
  6567.   rhs = expand_compound_operation (rhs);
  6568.   inner_code = GET_CODE (lhs);
  6569.   if (inner_code != GET_CODE (rhs))
  6570.     return x;
  6571.  
  6572.   /* See if the inner and outer operations distribute.  */
  6573.   switch (inner_code)
  6574.     {
  6575.     case LSHIFTRT:
  6576.     case ASHIFTRT:
  6577.     case AND:
  6578.     case IOR:
  6579.       /* These all distribute except over PLUS.  */
  6580.       if (code == PLUS || code == MINUS)
  6581.     return x;
  6582.       break;
  6583.  
  6584.     case MULT:
  6585.       if (code != PLUS && code != MINUS)
  6586.     return x;
  6587.       break;
  6588.  
  6589.     case ASHIFT:
  6590.       /* This is also a multiply, so it distributes over everything.  */
  6591.       break;
  6592.  
  6593.     case SUBREG:
  6594.       /* Non-paradoxical SUBREGs distributes over all operations, provided
  6595.      the inner modes and word numbers are the same, this is an extraction
  6596.      of a low-order part, we don't convert an fp operation to int or
  6597.      vice versa, and we would not be converting a single-word
  6598.      operation into a multi-word operation.  The latter test is not
  6599.      required, but it prevents generating unneeded multi-word operations.
  6600.      Some of the previous tests are redundant given the latter test, but
  6601.      are retained because they are required for correctness.
  6602.  
  6603.      We produce the result slightly differently in this case.  */
  6604.  
  6605.       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
  6606.       || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
  6607.       || ! subreg_lowpart_p (lhs)
  6608.       || (GET_MODE_CLASS (GET_MODE (lhs))
  6609.           != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
  6610.       || (GET_MODE_SIZE (GET_MODE (lhs))
  6611.           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
  6612.       || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
  6613.     return x;
  6614.  
  6615.       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
  6616.             SUBREG_REG (lhs), SUBREG_REG (rhs));
  6617.       return gen_lowpart_for_combine (GET_MODE (x), tem);
  6618.  
  6619.     default:
  6620.       return x;
  6621.     }
  6622.  
  6623.   /* Set LHS and RHS to the inner operands (A and B in the example
  6624.      above) and set OTHER to the common operand (C in the example).
  6625.      These is only one way to do this unless the inner operation is
  6626.      commutative.  */
  6627.   if (GET_RTX_CLASS (inner_code) == 'c'
  6628.       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
  6629.     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
  6630.   else if (GET_RTX_CLASS (inner_code) == 'c'
  6631.        && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
  6632.     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
  6633.   else if (GET_RTX_CLASS (inner_code) == 'c'
  6634.        && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
  6635.     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
  6636.   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
  6637.     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
  6638.   else
  6639.     return x;
  6640.  
  6641.   /* Form the new inner operation, seeing if it simplifies first.  */
  6642.   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
  6643.  
  6644.   /* There is one exception to the general way of distributing:
  6645.      (a ^ b) | (a ^ c) -> (~a) & (b ^ c)  */
  6646.   if (code == XOR && inner_code == IOR)
  6647.     {
  6648.       inner_code = AND;
  6649.       other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
  6650.     }
  6651.  
  6652.   /* We may be able to continuing distributing the result, so call
  6653.      ourselves recursively on the inner operation before forming the
  6654.      outer operation, which we return.  */
  6655.   return gen_binary (inner_code, GET_MODE (x),
  6656.              apply_distributive_law (tem), other);
  6657. }
  6658.  
  6659. /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
  6660.    in MODE.
  6661.  
  6662.    Return an equivalent form, if different from X.  Otherwise, return X.  If
  6663.    X is zero, we are to always construct the equivalent form.  */
  6664.  
  6665. static rtx
  6666. simplify_and_const_int (x, mode, varop, constop)
  6667.      rtx x;
  6668.      enum machine_mode mode;
  6669.      rtx varop;
  6670.      unsigned HOST_WIDE_INT constop;
  6671. {
  6672.   unsigned HOST_WIDE_INT nonzero;
  6673.   int width = GET_MODE_BITSIZE (mode);
  6674.   int i;
  6675.  
  6676.   /* Simplify VAROP knowing that we will be only looking at some of the
  6677.      bits in it.  */
  6678.   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
  6679.  
  6680.   /* If VAROP is a CLOBBER, we will fail so return it; if it is a
  6681.      CONST_INT, we are done.  */
  6682.   if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
  6683.     return varop;
  6684.  
  6685.   /* See what bits may be nonzero in VAROP.  Unlike the general case of
  6686.      a call to nonzero_bits, here we don't care about bits outside
  6687.      MODE.  */
  6688.  
  6689.   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
  6690.  
  6691.   /* If this would be an entire word for the target, but is not for
  6692.      the host, then sign-extend on the host so that the number will look
  6693.      the same way on the host that it would on the target.
  6694.  
  6695.      For example, when building a 64 bit alpha hosted 32 bit sparc
  6696.      targeted compiler, then we want the 32 bit unsigned value -1 to be
  6697.      represented as a 64 bit value -1, and not as 0x00000000ffffffff.
  6698.      The later confuses the sparc backend.  */
  6699.  
  6700.   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
  6701.       && (nonzero & ((HOST_WIDE_INT) 1 << (width - 1))))
  6702.     nonzero |= ((HOST_WIDE_INT) (-1) << width);
  6703.  
  6704.   /* Turn off all bits in the constant that are known to already be zero.
  6705.      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
  6706.      which is tested below.  */
  6707.  
  6708.   constop &= nonzero;
  6709.  
  6710.   /* If we don't have any bits left, return zero.  */
  6711.   if (constop == 0)
  6712.     return const0_rtx;
  6713.  
  6714.   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
  6715.      a power of two, we can replace this with a ASHIFT.  */
  6716.   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
  6717.       && (i = exact_log2 (constop)) >= 0)
  6718.     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
  6719.                  
  6720.   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
  6721.      or XOR, then try to apply the distributive law.  This may eliminate
  6722.      operations if either branch can be simplified because of the AND.
  6723.      It may also make some cases more complex, but those cases probably
  6724.      won't match a pattern either with or without this.  */
  6725.  
  6726.   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
  6727.     return
  6728.       gen_lowpart_for_combine
  6729.     (mode,
  6730.      apply_distributive_law
  6731.      (gen_binary (GET_CODE (varop), GET_MODE (varop),
  6732.               simplify_and_const_int (NULL_RTX, GET_MODE (varop),
  6733.                           XEXP (varop, 0), constop),
  6734.               simplify_and_const_int (NULL_RTX, GET_MODE (varop),
  6735.                           XEXP (varop, 1), constop))));
  6736.  
  6737.   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
  6738.      if we already had one (just check for the simplest cases).  */
  6739.   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
  6740.       && GET_MODE (XEXP (x, 0)) == mode
  6741.       && SUBREG_REG (XEXP (x, 0)) == varop)
  6742.     varop = XEXP (x, 0);
  6743.   else
  6744.     varop = gen_lowpart_for_combine (mode, varop);
  6745.  
  6746.   /* If we can't make the SUBREG, try to return what we were given. */
  6747.   if (GET_CODE (varop) == CLOBBER)
  6748.     return x ? x : varop;
  6749.  
  6750.   /* If we are only masking insignificant bits, return VAROP.  */
  6751.   if (constop == nonzero)
  6752.     x = varop;
  6753.  
  6754.   /* Otherwise, return an AND.  See how much, if any, of X we can use.  */
  6755.   else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
  6756.     x = gen_binary (AND, mode, varop, GEN_INT (constop));
  6757.  
  6758.   else
  6759.     {
  6760.       if (GET_CODE (XEXP (x, 1)) != CONST_INT
  6761.       || INTVAL (XEXP (x, 1)) != constop)
  6762.     SUBST (XEXP (x, 1), GEN_INT (constop));
  6763.  
  6764.       SUBST (XEXP (x, 0), varop);
  6765.     }
  6766.  
  6767.   return x;
  6768. }
  6769.  
  6770. /* Given an expression, X, compute which bits in X can be non-zero.
  6771.    We don't care about bits outside of those defined in MODE.
  6772.  
  6773.    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
  6774.    a shift, AND, or zero_extract, we can do better.  */
  6775.  
  6776. static unsigned HOST_WIDE_INT
  6777. nonzero_bits (x, mode)
  6778.      rtx x;
  6779.      enum machine_mode mode;
  6780. {
  6781.   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
  6782.   unsigned HOST_WIDE_INT inner_nz;
  6783.   enum rtx_code code;
  6784.   int mode_width = GET_MODE_BITSIZE (mode);
  6785.   rtx tem;
  6786.  
  6787.   /* For floating-point values, assume all bits are needed.  */
  6788.   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
  6789.     return nonzero;
  6790.  
  6791.   /* If X is wider than MODE, use its mode instead.  */
  6792.   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
  6793.     {
  6794.       mode = GET_MODE (x);
  6795.       nonzero = GET_MODE_MASK (mode);
  6796.       mode_width = GET_MODE_BITSIZE (mode);
  6797.     }
  6798.  
  6799.   if (mode_width > HOST_BITS_PER_WIDE_INT)
  6800.     /* Our only callers in this case look for single bit values.  So
  6801.        just return the mode mask.  Those tests will then be false.  */
  6802.     return nonzero;
  6803.  
  6804. #ifndef WORD_REGISTER_OPERATIONS
  6805.   /* If MODE is wider than X, but both are a single word for both the host
  6806.      and target machines, we can compute this from which bits of the 
  6807.      object might be nonzero in its own mode, taking into account the fact
  6808.      that on many CISC machines, accessing an object in a wider mode
  6809.      causes the high-order bits to become undefined.  So they are
  6810.      not known to be zero.  */
  6811.  
  6812.   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
  6813.       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
  6814.       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
  6815.       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
  6816.     {
  6817.       nonzero &= nonzero_bits (x, GET_MODE (x));
  6818.       nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
  6819.       return nonzero;
  6820.     }
  6821. #endif
  6822.  
  6823.   code = GET_CODE (x);
  6824.   switch (code)
  6825.     {
  6826.     case REG:
  6827. #ifdef STACK_BOUNDARY
  6828.       /* If this is the stack pointer, we may know something about its
  6829.      alignment.  If PUSH_ROUNDING is defined, it is possible for the
  6830.      stack to be momentarily aligned only to that amount, so we pick
  6831.      the least alignment.  */
  6832.  
  6833.       if (x == stack_pointer_rtx)
  6834.     {
  6835.       int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
  6836.  
  6837. #ifdef PUSH_ROUNDING
  6838.       sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
  6839. #endif
  6840.  
  6841.       nonzero &= ~ (sp_alignment - 1);
  6842.     }
  6843. #endif
  6844.  
  6845. #ifdef POINTERS_EXTEND_UNSIGNED
  6846.       /* If pointers extend unsigned and this is a pointer in Pmode, say that
  6847.      all the bits above ptr_mode are known to be zero.  */
  6848.       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
  6849.       && REGNO_POINTER_FLAG (REGNO (x)))
  6850.     nonzero &= GET_MODE_MASK (ptr_mode);
  6851. #endif
  6852.  
  6853.       /* If X is a register whose nonzero bits value is current, use it.
  6854.      Otherwise, if X is a register whose value we can find, use that
  6855.      value.  Otherwise, use the previously-computed global nonzero bits
  6856.      for this register.  */
  6857.  
  6858.       if (reg_last_set_value[REGNO (x)] != 0
  6859.       && reg_last_set_mode[REGNO (x)] == mode
  6860.       && (reg_n_sets[REGNO (x)] == 1
  6861.           || reg_last_set_label[REGNO (x)] == label_tick)
  6862.       && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
  6863.     return reg_last_set_nonzero_bits[REGNO (x)];
  6864.  
  6865.       tem = get_last_value (x);
  6866.  
  6867.       if (tem)
  6868.     {
  6869. #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
  6870.       /* If X is narrower than MODE and TEM is a non-negative
  6871.          constant that would appear negative in the mode of X,
  6872.          sign-extend it for use in reg_nonzero_bits because some
  6873.          machines (maybe most) will actually do the sign-extension
  6874.          and this is the conservative approach. 
  6875.  
  6876.          ??? For 2.5, try to tighten up the MD files in this regard
  6877.          instead of this kludge.  */
  6878.  
  6879.       if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
  6880.           && GET_CODE (tem) == CONST_INT
  6881.           && INTVAL (tem) > 0
  6882.           && 0 != (INTVAL (tem)
  6883.                & ((HOST_WIDE_INT) 1
  6884.               << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
  6885.         tem = GEN_INT (INTVAL (tem)
  6886.                | ((HOST_WIDE_INT) (-1)
  6887.                   << GET_MODE_BITSIZE (GET_MODE (x))));
  6888. #endif
  6889.       return nonzero_bits (tem, mode);
  6890.     }
  6891.       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
  6892.     return reg_nonzero_bits[REGNO (x)] & nonzero;
  6893.       else
  6894.     return nonzero;
  6895.  
  6896.     case CONST_INT:
  6897. #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
  6898.       /* If X is negative in MODE, sign-extend the value.  */
  6899.       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
  6900.       && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
  6901.     return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
  6902. #endif
  6903.  
  6904.       return INTVAL (x);
  6905.  
  6906.     case MEM:
  6907. #ifdef LOAD_EXTEND_OP
  6908.       /* In many, if not most, RISC machines, reading a byte from memory
  6909.      zeros the rest of the register.  Noticing that fact saves a lot
  6910.      of extra zero-extends.  */
  6911.       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
  6912.     nonzero &= GET_MODE_MASK (GET_MODE (x));
  6913. #endif
  6914.       break;
  6915.  
  6916.     case EQ:  case NE:
  6917.     case GT:  case GTU:
  6918.     case LT:  case LTU:
  6919.     case GE:  case GEU:
  6920.     case LE:  case LEU:
  6921.  
  6922.       /* If this produces an integer result, we know which bits are set.
  6923.      Code here used to clear bits outside the mode of X, but that is
  6924.      now done above.  */
  6925.  
  6926.       if (GET_MODE_CLASS (mode) == MODE_INT
  6927.       && mode_width <= HOST_BITS_PER_WIDE_INT)
  6928.     nonzero = STORE_FLAG_VALUE;
  6929.       break;
  6930.  
  6931.     case NEG:
  6932.       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
  6933.       == GET_MODE_BITSIZE (GET_MODE (x)))
  6934.     nonzero = 1;
  6935.  
  6936.       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
  6937.     nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
  6938.       break;
  6939.  
  6940.     case ABS:
  6941.       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
  6942.       == GET_MODE_BITSIZE (GET_MODE (x)))
  6943.     nonzero = 1;
  6944.       break;
  6945.  
  6946.     case TRUNCATE:
  6947.       nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
  6948.       break;
  6949.  
  6950.     case ZERO_EXTEND:
  6951.       nonzero &= nonzero_bits (XEXP (x, 0), mode);
  6952.       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
  6953.     nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
  6954.       break;
  6955.  
  6956.     case SIGN_EXTEND:
  6957.       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
  6958.      Otherwise, show all the bits in the outer mode but not the inner
  6959.      may be non-zero.  */
  6960.       inner_nz = nonzero_bits (XEXP (x, 0), mode);
  6961.       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
  6962.     {
  6963.       inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
  6964.       if (inner_nz &
  6965.           (((HOST_WIDE_INT) 1
  6966.         << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
  6967.         inner_nz |= (GET_MODE_MASK (mode)
  6968.               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
  6969.     }
  6970.  
  6971.       nonzero &= inner_nz;
  6972.       break;
  6973.  
  6974.     case AND:
  6975.       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
  6976.           & nonzero_bits (XEXP (x, 1), mode));
  6977.       break;
  6978.  
  6979.     case XOR:   case IOR:
  6980.     case UMIN:  case UMAX:  case SMIN:  case SMAX:
  6981.       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
  6982.           | nonzero_bits (XEXP (x, 1), mode));
  6983.       break;
  6984.  
  6985.     case PLUS:  case MINUS:
  6986.     case MULT:
  6987.     case DIV:   case UDIV:
  6988.     case MOD:   case UMOD:
  6989.       /* We can apply the rules of arithmetic to compute the number of
  6990.      high- and low-order zero bits of these operations.  We start by
  6991.      computing the width (position of the highest-order non-zero bit)
  6992.      and the number of low-order zero bits for each value.  */
  6993.       {
  6994.     unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
  6995.     unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
  6996.     int width0 = floor_log2 (nz0) + 1;
  6997.     int width1 = floor_log2 (nz1) + 1;
  6998.     int low0 = floor_log2 (nz0 & -nz0);
  6999.     int low1 = floor_log2 (nz1 & -nz1);
  7000.     HOST_WIDE_INT op0_maybe_minusp
  7001.       = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
  7002.     HOST_WIDE_INT op1_maybe_minusp
  7003.       = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
  7004.     int result_width = mode_width;
  7005.     int result_low = 0;
  7006.  
  7007.     switch (code)
  7008.       {
  7009.       case PLUS:
  7010.         result_width = MAX (width0, width1) + 1;
  7011.         result_low = MIN (low0, low1);
  7012.         break;
  7013.       case MINUS:
  7014.         result_low = MIN (low0, low1);
  7015.         break;
  7016.       case MULT:
  7017.         result_width = width0 + width1;
  7018.         result_low = low0 + low1;
  7019.         break;
  7020.       case DIV:
  7021.         if (! op0_maybe_minusp && ! op1_maybe_minusp)
  7022.           result_width = width0;
  7023.         break;
  7024.       case UDIV:
  7025.         result_width = width0;
  7026.         break;
  7027.       case MOD:
  7028.         if (! op0_maybe_minusp && ! op1_maybe_minusp)
  7029.           result_width = MIN (width0, width1);
  7030.         result_low = MIN (low0, low1);
  7031.         break;
  7032.       case UMOD:
  7033.         result_width = MIN (width0, width1);
  7034.         result_low = MIN (low0, low1);
  7035.         break;
  7036.       }
  7037.  
  7038.     if (result_width < mode_width)
  7039.       nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
  7040.  
  7041.     if (result_low > 0)
  7042.       nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
  7043.       }
  7044.       break;
  7045.  
  7046.     case ZERO_EXTRACT:
  7047.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  7048.       && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
  7049.     nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
  7050.       break;
  7051.  
  7052.     case SUBREG:
  7053.       /* If this is a SUBREG formed for a promoted variable that has
  7054.      been zero-extended, we know that at least the high-order bits
  7055.      are zero, though others might be too.  */
  7056.  
  7057.       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
  7058.     nonzero = (GET_MODE_MASK (GET_MODE (x))
  7059.            & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
  7060.  
  7061.       /* If the inner mode is a single word for both the host and target
  7062.      machines, we can compute this from which bits of the inner
  7063.      object might be nonzero.  */
  7064.       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
  7065.       && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
  7066.           <= HOST_BITS_PER_WIDE_INT))
  7067.     {
  7068.       nonzero &= nonzero_bits (SUBREG_REG (x), mode);
  7069.  
  7070. #ifndef WORD_REGISTER_OPERATIONS
  7071.       /* On many CISC machines, accessing an object in a wider mode
  7072.          causes the high-order bits to become undefined.  So they are
  7073.          not known to be zero.  */
  7074.       if (GET_MODE_SIZE (GET_MODE (x))
  7075.           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
  7076.         nonzero |= (GET_MODE_MASK (GET_MODE (x))
  7077.             & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
  7078. #endif
  7079.     }
  7080.       break;
  7081.  
  7082.     case ASHIFTRT:
  7083.     case LSHIFTRT:
  7084.     case ASHIFT:
  7085.     case ROTATE:
  7086.       /* The nonzero bits are in two classes: any bits within MODE
  7087.      that aren't in GET_MODE (x) are always significant.  The rest of the
  7088.      nonzero bits are those that are significant in the operand of
  7089.      the shift when shifted the appropriate number of bits.  This
  7090.      shows that high-order bits are cleared by the right shift and
  7091.      low-order bits by left shifts.  */
  7092.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  7093.       && INTVAL (XEXP (x, 1)) >= 0
  7094.       && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
  7095.     {
  7096.       enum machine_mode inner_mode = GET_MODE (x);
  7097.       int width = GET_MODE_BITSIZE (inner_mode);
  7098.       int count = INTVAL (XEXP (x, 1));
  7099.       unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
  7100.       unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
  7101.       unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
  7102.       unsigned HOST_WIDE_INT outer = 0;
  7103.  
  7104.       if (mode_width > width)
  7105.         outer = (op_nonzero & nonzero & ~ mode_mask);
  7106.  
  7107.       if (code == LSHIFTRT)
  7108.         inner >>= count;
  7109.       else if (code == ASHIFTRT)
  7110.         {
  7111.           inner >>= count;
  7112.  
  7113.           /* If the sign bit may have been nonzero before the shift, we
  7114.          need to mark all the places it could have been copied to
  7115.          by the shift as possibly nonzero.  */
  7116.           if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
  7117.         inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
  7118.         }
  7119.       else if (code == ASHIFT)
  7120.         inner <<= count;
  7121.       else
  7122.         inner = ((inner << (count % width)
  7123.               | (inner >> (width - (count % width)))) & mode_mask);
  7124.  
  7125.       nonzero &= (outer | inner);
  7126.     }
  7127.       break;
  7128.  
  7129.     case FFS:
  7130.       /* This is at most the number of bits in the mode.  */
  7131.       nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
  7132.       break;
  7133.  
  7134.     case IF_THEN_ELSE:
  7135.       nonzero &= (nonzero_bits (XEXP (x, 1), mode)
  7136.           | nonzero_bits (XEXP (x, 2), mode));
  7137.       break;
  7138.     }
  7139.  
  7140.   return nonzero;
  7141. }
  7142.  
  7143. /* Return the number of bits at the high-order end of X that are known to
  7144.    be equal to the sign bit.  X will be used in mode MODE; if MODE is
  7145.    VOIDmode, X will be used in its own mode.  The returned value  will always
  7146.    be between 1 and the number of bits in MODE.  */
  7147.  
  7148. static int
  7149. num_sign_bit_copies (x, mode)
  7150.      rtx x;
  7151.      enum machine_mode mode;
  7152. {
  7153.   enum rtx_code code = GET_CODE (x);
  7154.   int bitwidth;
  7155.   int num0, num1, result;
  7156.   unsigned HOST_WIDE_INT nonzero;
  7157.   rtx tem;
  7158.  
  7159.   /* If we weren't given a mode, use the mode of X.  If the mode is still
  7160.      VOIDmode, we don't know anything.  Likewise if one of the modes is
  7161.      floating-point.  */
  7162.  
  7163.   if (mode == VOIDmode)
  7164.     mode = GET_MODE (x);
  7165.  
  7166.   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
  7167.     return 1;
  7168.  
  7169.   bitwidth = GET_MODE_BITSIZE (mode);
  7170.  
  7171.   /* For a smaller object, just ignore the high bits. */
  7172.   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
  7173.     return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
  7174.             - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
  7175.      
  7176. #ifndef WORD_REGISTER_OPERATIONS
  7177.   /* If this machine does not do all register operations on the entire
  7178.      register and MODE is wider than the mode of X, we can say nothing
  7179.      at all about the high-order bits.  */
  7180.   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
  7181.     return 1;
  7182. #endif
  7183.  
  7184.   switch (code)
  7185.     {
  7186.     case REG:
  7187.  
  7188. #ifdef POINTERS_EXTEND_UNSIGNED
  7189.       /* If pointers extend signed and this is a pointer in Pmode, say that
  7190.      all the bits above ptr_mode are known to be sign bit copies.  */
  7191.       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
  7192.       && REGNO_POINTER_FLAG (REGNO (x)))
  7193.     return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
  7194. #endif
  7195.  
  7196.       if (reg_last_set_value[REGNO (x)] != 0
  7197.       && reg_last_set_mode[REGNO (x)] == mode
  7198.       && (reg_n_sets[REGNO (x)] == 1
  7199.           || reg_last_set_label[REGNO (x)] == label_tick)
  7200.       && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
  7201.     return reg_last_set_sign_bit_copies[REGNO (x)];
  7202.  
  7203.       tem =  get_last_value (x);
  7204.       if (tem != 0)
  7205.     return num_sign_bit_copies (tem, mode);
  7206.  
  7207.       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
  7208.     return reg_sign_bit_copies[REGNO (x)];
  7209.       break;
  7210.  
  7211.     case MEM:
  7212. #ifdef LOAD_EXTEND_OP
  7213.       /* Some RISC machines sign-extend all loads of smaller than a word.  */
  7214.       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
  7215.     return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
  7216. #endif
  7217.       break;
  7218.  
  7219.     case CONST_INT:
  7220.       /* If the constant is negative, take its 1's complement and remask.
  7221.      Then see how many zero bits we have.  */
  7222.       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
  7223.       if (bitwidth <= HOST_BITS_PER_WIDE_INT
  7224.       && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
  7225.     nonzero = (~ nonzero) & GET_MODE_MASK (mode);
  7226.  
  7227.       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
  7228.  
  7229.     case SUBREG:
  7230.       /* If this is a SUBREG for a promoted object that is sign-extended
  7231.      and we are looking at it in a wider mode, we know that at least the
  7232.      high-order bits are known to be sign bit copies.  */
  7233.  
  7234.       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
  7235.     return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
  7236.             num_sign_bit_copies (SUBREG_REG (x), mode));
  7237.  
  7238.       /* For a smaller object, just ignore the high bits. */
  7239.       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
  7240.     {
  7241.       num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
  7242.       return MAX (1, (num0
  7243.               - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
  7244.                  - bitwidth)));
  7245.     }
  7246.  
  7247. #ifdef WORD_REGISTER_OPERATIONS
  7248. #ifdef LOAD_EXTEND_OP
  7249.       /* For paradoxical SUBREGs on machines where all register operations
  7250.      affect the entire register, just look inside.  Note that we are
  7251.      passing MODE to the recursive call, so the number of sign bit copies
  7252.      will remain relative to that mode, not the inner mode.  */
  7253.  
  7254.       /* This works only if loads sign extend.  Otherwise, if we get a
  7255.      reload for the inner part, it may be loaded from the stack, and
  7256.      then we lose all sign bit copies that existed before the store
  7257.      to the stack.  */
  7258.  
  7259.       if ((GET_MODE_SIZE (GET_MODE (x))
  7260.        > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
  7261.       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
  7262.     return num_sign_bit_copies (SUBREG_REG (x), mode);
  7263. #endif
  7264. #endif
  7265.       break;
  7266.  
  7267.     case SIGN_EXTRACT:
  7268.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  7269.     return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
  7270.       break;
  7271.  
  7272.     case SIGN_EXTEND: 
  7273.       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
  7274.           + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
  7275.  
  7276.     case TRUNCATE:
  7277.       /* For a smaller object, just ignore the high bits. */
  7278.       num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
  7279.       return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
  7280.                   - bitwidth)));
  7281.  
  7282.     case NOT:
  7283.       return num_sign_bit_copies (XEXP (x, 0), mode);
  7284.  
  7285.     case ROTATE:       case ROTATERT:
  7286.       /* If we are rotating left by a number of bits less than the number
  7287.      of sign bit copies, we can just subtract that amount from the
  7288.      number.  */
  7289.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  7290.       && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
  7291.     {
  7292.       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
  7293.       return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
  7294.                  : bitwidth - INTVAL (XEXP (x, 1))));
  7295.     }
  7296.       break;
  7297.  
  7298.     case NEG:
  7299.       /* In general, this subtracts one sign bit copy.  But if the value
  7300.      is known to be positive, the number of sign bit copies is the
  7301.      same as that of the input.  Finally, if the input has just one bit
  7302.      that might be nonzero, all the bits are copies of the sign bit.  */
  7303.       nonzero = nonzero_bits (XEXP (x, 0), mode);
  7304.       if (nonzero == 1)
  7305.     return bitwidth;
  7306.  
  7307.       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
  7308.       if (num0 > 1
  7309.       && bitwidth <= HOST_BITS_PER_WIDE_INT
  7310.       && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
  7311.     num0--;
  7312.  
  7313.       return num0;
  7314.  
  7315.     case IOR:   case AND:   case XOR:
  7316.     case SMIN:  case SMAX:  case UMIN:  case UMAX:
  7317.       /* Logical operations will preserve the number of sign-bit copies.
  7318.      MIN and MAX operations always return one of the operands.  */
  7319.       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
  7320.       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
  7321.       return MIN (num0, num1);
  7322.  
  7323.     case PLUS:  case MINUS:
  7324.       /* For addition and subtraction, we can have a 1-bit carry.  However,
  7325.      if we are subtracting 1 from a positive number, there will not
  7326.      be such a carry.  Furthermore, if the positive number is known to
  7327.      be 0 or 1, we know the result is either -1 or 0.  */
  7328.  
  7329.       if (code == PLUS && XEXP (x, 1) == constm1_rtx
  7330.       && bitwidth <= HOST_BITS_PER_WIDE_INT)
  7331.     {
  7332.       nonzero = nonzero_bits (XEXP (x, 0), mode);
  7333.       if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
  7334.         return (nonzero == 1 || nonzero == 0 ? bitwidth
  7335.             : bitwidth - floor_log2 (nonzero) - 1);
  7336.     }
  7337.  
  7338.       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
  7339.       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
  7340.       return MAX (1, MIN (num0, num1) - 1);
  7341.       
  7342.     case MULT:
  7343.       /* The number of bits of the product is the sum of the number of
  7344.      bits of both terms.  However, unless one of the terms if known
  7345.      to be positive, we must allow for an additional bit since negating
  7346.      a negative number can remove one sign bit copy.  */
  7347.  
  7348.       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
  7349.       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
  7350.  
  7351.       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
  7352.       if (result > 0
  7353.       && bitwidth <= HOST_BITS_PER_WIDE_INT
  7354.       && ((nonzero_bits (XEXP (x, 0), mode)
  7355.            & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
  7356.       && (nonzero_bits (XEXP (x, 1), mode)
  7357.           & ((HOST_WIDE_INT) 1 << (bitwidth - 1)) != 0))
  7358.     result--;
  7359.  
  7360.       return MAX (1, result);
  7361.  
  7362.     case UDIV:
  7363.       /* The result must be <= the first operand.  */
  7364.       return num_sign_bit_copies (XEXP (x, 0), mode);
  7365.  
  7366.     case UMOD:
  7367.       /* The result must be <= the scond operand.  */
  7368.       return num_sign_bit_copies (XEXP (x, 1), mode);
  7369.  
  7370.     case DIV:
  7371.       /* Similar to unsigned division, except that we have to worry about
  7372.      the case where the divisor is negative, in which case we have
  7373.      to add 1.  */
  7374.       result = num_sign_bit_copies (XEXP (x, 0), mode);
  7375.       if (result > 1
  7376.       && bitwidth <= HOST_BITS_PER_WIDE_INT
  7377.       && (nonzero_bits (XEXP (x, 1), mode)
  7378.           & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
  7379.     result --;
  7380.  
  7381.       return result;
  7382.  
  7383.     case MOD:
  7384.       result = num_sign_bit_copies (XEXP (x, 1), mode);
  7385.       if (result > 1
  7386.       && bitwidth <= HOST_BITS_PER_WIDE_INT
  7387.       && (nonzero_bits (XEXP (x, 1), mode)
  7388.           & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
  7389.     result --;
  7390.  
  7391.       return result;
  7392.  
  7393.     case ASHIFTRT:
  7394.       /* Shifts by a constant add to the number of bits equal to the
  7395.      sign bit.  */
  7396.       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
  7397.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  7398.       && INTVAL (XEXP (x, 1)) > 0)
  7399.     num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
  7400.  
  7401.       return num0;
  7402.  
  7403.     case ASHIFT:
  7404.       /* Left shifts destroy copies.  */
  7405.       if (GET_CODE (XEXP (x, 1)) != CONST_INT
  7406.       || INTVAL (XEXP (x, 1)) < 0
  7407.       || INTVAL (XEXP (x, 1)) >= bitwidth)
  7408.     return 1;
  7409.  
  7410.       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
  7411.       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
  7412.  
  7413.     case IF_THEN_ELSE:
  7414.       num0 = num_sign_bit_copies (XEXP (x, 1), mode);
  7415.       num1 = num_sign_bit_copies (XEXP (x, 2), mode);
  7416.       return MIN (num0, num1);
  7417.  
  7418. #if STORE_FLAG_VALUE == -1
  7419.     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
  7420.     case GEU: case GTU: case LEU: case LTU:
  7421.       return bitwidth;
  7422. #endif
  7423.     }
  7424.  
  7425.   /* If we haven't been able to figure it out by one of the above rules,
  7426.      see if some of the high-order bits are known to be zero.  If so,
  7427.      count those bits and return one less than that amount.  If we can't
  7428.      safely compute the mask for this mode, always return BITWIDTH.  */
  7429.  
  7430.   if (bitwidth > HOST_BITS_PER_WIDE_INT)
  7431.     return 1;
  7432.  
  7433.   nonzero = nonzero_bits (x, mode);
  7434.   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
  7435.       ? 1 : bitwidth - floor_log2 (nonzero) - 1);
  7436. }
  7437.  
  7438. /* Return the number of "extended" bits there are in X, when interpreted
  7439.    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
  7440.    unsigned quantities, this is the number of high-order zero bits.
  7441.    For signed quantities, this is the number of copies of the sign bit
  7442.    minus 1.  In both case, this function returns the number of "spare"
  7443.    bits.  For example, if two quantities for which this function returns
  7444.    at least 1 are added, the addition is known not to overflow.
  7445.  
  7446.    This function will always return 0 unless called during combine, which
  7447.    implies that it must be called from a define_split.  */
  7448.  
  7449. int
  7450. extended_count (x, mode, unsignedp)
  7451.      rtx x;
  7452.      enum machine_mode mode;
  7453.      int unsignedp;
  7454. {
  7455.   if (nonzero_sign_valid == 0)
  7456.     return 0;
  7457.  
  7458.   return (unsignedp
  7459.       ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  7460.          && (GET_MODE_BITSIZE (mode) - 1
  7461.          - floor_log2 (nonzero_bits (x, mode))))
  7462.       : num_sign_bit_copies (x, mode) - 1);
  7463. }
  7464.  
  7465. /* This function is called from `simplify_shift_const' to merge two
  7466.    outer operations.  Specifically, we have already found that we need
  7467.    to perform operation *POP0 with constant *PCONST0 at the outermost
  7468.    position.  We would now like to also perform OP1 with constant CONST1
  7469.    (with *POP0 being done last).
  7470.  
  7471.    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
  7472.    the resulting operation.  *PCOMP_P is set to 1 if we would need to 
  7473.    complement the innermost operand, otherwise it is unchanged.
  7474.  
  7475.    MODE is the mode in which the operation will be done.  No bits outside
  7476.    the width of this mode matter.  It is assumed that the width of this mode
  7477.    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
  7478.  
  7479.    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
  7480.    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
  7481.    result is simply *PCONST0.
  7482.  
  7483.    If the resulting operation cannot be expressed as one operation, we
  7484.    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
  7485.  
  7486. static int
  7487. merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
  7488.      enum rtx_code *pop0;
  7489.      HOST_WIDE_INT *pconst0;
  7490.      enum rtx_code op1;
  7491.      HOST_WIDE_INT const1;
  7492.      enum machine_mode mode;
  7493.      int *pcomp_p;
  7494. {
  7495.   enum rtx_code op0 = *pop0;
  7496.   HOST_WIDE_INT const0 = *pconst0;
  7497.   int width = GET_MODE_BITSIZE (mode);
  7498.  
  7499.   const0 &= GET_MODE_MASK (mode);
  7500.   const1 &= GET_MODE_MASK (mode);
  7501.  
  7502.   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
  7503.   if (op0 == AND)
  7504.     const1 &= const0;
  7505.  
  7506.   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
  7507.      if OP0 is SET.  */
  7508.  
  7509.   if (op1 == NIL || op0 == SET)
  7510.     return 1;
  7511.  
  7512.   else if (op0 == NIL)
  7513.     op0 = op1, const0 = const1;
  7514.  
  7515.   else if (op0 == op1)
  7516.     {
  7517.       switch (op0)
  7518.     {
  7519.     case AND:
  7520.       const0 &= const1;
  7521.       break;
  7522.     case IOR:
  7523.       const0 |= const1;
  7524.       break;
  7525.     case XOR:
  7526.       const0 ^= const1;
  7527.       break;
  7528.     case PLUS:
  7529.       const0 += const1;
  7530.       break;
  7531.     case NEG:
  7532.       op0 = NIL;
  7533.       break;
  7534.     }
  7535.     }
  7536.  
  7537.   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
  7538.   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
  7539.     return 0;
  7540.  
  7541.   /* If the two constants aren't the same, we can't do anything.  The
  7542.      remaining six cases can all be done.  */
  7543.   else if (const0 != const1)
  7544.     return 0;
  7545.  
  7546.   else
  7547.     switch (op0)
  7548.       {
  7549.       case IOR:
  7550.     if (op1 == AND)
  7551.       /* (a & b) | b == b */
  7552.       op0 = SET;
  7553.     else /* op1 == XOR */
  7554.       /* (a ^ b) | b == a | b */
  7555.       ;
  7556.     break;
  7557.  
  7558.       case XOR:
  7559.     if (op1 == AND)
  7560.       /* (a & b) ^ b == (~a) & b */
  7561.       op0 = AND, *pcomp_p = 1;
  7562.     else /* op1 == IOR */
  7563.       /* (a | b) ^ b == a & ~b */
  7564.       op0 = AND, *pconst0 = ~ const0;
  7565.     break;
  7566.  
  7567.       case AND:
  7568.     if (op1 == IOR)
  7569.       /* (a | b) & b == b */
  7570.     op0 = SET;
  7571.     else /* op1 == XOR */
  7572.       /* (a ^ b) & b) == (~a) & b */
  7573.       *pcomp_p = 1;
  7574.     break;
  7575.       }
  7576.  
  7577.   /* Check for NO-OP cases.  */
  7578.   const0 &= GET_MODE_MASK (mode);
  7579.   if (const0 == 0
  7580.       && (op0 == IOR || op0 == XOR || op0 == PLUS))
  7581.     op0 = NIL;
  7582.   else if (const0 == 0 && op0 == AND)
  7583.     op0 = SET;
  7584.   else if (const0 == GET_MODE_MASK (mode) && op0 == AND)
  7585.     op0 = NIL;
  7586.  
  7587.   /* If this would be an entire word for the target, but is not for
  7588.      the host, then sign-extend on the host so that the number will look
  7589.      the same way on the host that it would on the target.
  7590.  
  7591.      For example, when building a 64 bit alpha hosted 32 bit sparc
  7592.      targeted compiler, then we want the 32 bit unsigned value -1 to be
  7593.      represented as a 64 bit value -1, and not as 0x00000000ffffffff.
  7594.      The later confuses the sparc backend.  */
  7595.  
  7596.   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
  7597.       && (const0 & ((HOST_WIDE_INT) 1 << (width - 1))))
  7598.     const0 |= ((HOST_WIDE_INT) (-1) << width);
  7599.  
  7600.   *pop0 = op0;
  7601.   *pconst0 = const0;
  7602.  
  7603.   return 1;
  7604. }
  7605.  
  7606. /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
  7607.    The result of the shift is RESULT_MODE.  X, if non-zero, is an expression
  7608.    that we started with.
  7609.  
  7610.    The shift is normally computed in the widest mode we find in VAROP, as
  7611.    long as it isn't a different number of words than RESULT_MODE.  Exceptions
  7612.    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
  7613.  
  7614. static rtx
  7615. simplify_shift_const (x, code, result_mode, varop, count)
  7616.      rtx x;
  7617.      enum rtx_code code;
  7618.      enum machine_mode result_mode;
  7619.      rtx varop;
  7620.      int count;
  7621. {
  7622.   enum rtx_code orig_code = code;
  7623.   int orig_count = count;
  7624.   enum machine_mode mode = result_mode;
  7625.   enum machine_mode shift_mode, tmode;
  7626.   int mode_words
  7627.     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
  7628.   /* We form (outer_op (code varop count) (outer_const)).  */
  7629.   enum rtx_code outer_op = NIL;
  7630.   HOST_WIDE_INT outer_const = 0;
  7631.   rtx const_rtx;
  7632.   int complement_p = 0;
  7633.   rtx new;
  7634.  
  7635.   /* If we were given an invalid count, don't do anything except exactly
  7636.      what was requested.  */
  7637.  
  7638.   if (count < 0 || count > GET_MODE_BITSIZE (mode))
  7639.     {
  7640.       if (x)
  7641.     return x;
  7642.  
  7643.       return gen_rtx (code, mode, varop, GEN_INT (count));
  7644.     }
  7645.  
  7646.   /* Unless one of the branches of the `if' in this loop does a `continue',
  7647.      we will `break' the loop after the `if'.  */
  7648.  
  7649.   while (count != 0)
  7650.     {
  7651.       /* If we have an operand of (clobber (const_int 0)), just return that
  7652.      value.  */
  7653.       if (GET_CODE (varop) == CLOBBER)
  7654.     return varop;
  7655.  
  7656.       /* If we discovered we had to complement VAROP, leave.  Making a NOT
  7657.      here would cause an infinite loop.  */
  7658.       if (complement_p)
  7659.     break;
  7660.  
  7661.       /* Convert ROTATETRT to ROTATE.  */
  7662.       if (code == ROTATERT)
  7663.     code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
  7664.  
  7665.       /* We need to determine what mode we will do the shift in.  If the
  7666.      shift is a right shift or a ROTATE, we must always do it in the mode
  7667.      it was originally done in.  Otherwise, we can do it in MODE, the
  7668.      widest mode encountered. */
  7669.       shift_mode
  7670.     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
  7671.        ? result_mode : mode);
  7672.  
  7673.       /* Handle cases where the count is greater than the size of the mode
  7674.      minus 1.  For ASHIFT, use the size minus one as the count (this can
  7675.      occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
  7676.      take the count modulo the size.  For other shifts, the result is
  7677.      zero.
  7678.  
  7679.      Since these shifts are being produced by the compiler by combining
  7680.      multiple operations, each of which are defined, we know what the
  7681.      result is supposed to be.  */
  7682.      
  7683.       if (count > GET_MODE_BITSIZE (shift_mode) - 1)
  7684.     {
  7685.       if (code == ASHIFTRT)
  7686.         count = GET_MODE_BITSIZE (shift_mode) - 1;
  7687.       else if (code == ROTATE || code == ROTATERT)
  7688.         count %= GET_MODE_BITSIZE (shift_mode);
  7689.       else
  7690.         {
  7691.           /* We can't simply return zero because there may be an
  7692.          outer op.  */
  7693.           varop = const0_rtx;
  7694.           count = 0;
  7695.           break;
  7696.         }
  7697.     }
  7698.  
  7699.       /* Negative counts are invalid and should not have been made (a
  7700.      programmer-specified negative count should have been handled
  7701.      above). */
  7702.       else if (count < 0)
  7703.     abort ();
  7704.  
  7705.       /* An arithmetic right shift of a quantity known to be -1 or 0
  7706.      is a no-op.  */
  7707.       if (code == ASHIFTRT
  7708.       && (num_sign_bit_copies (varop, shift_mode)
  7709.           == GET_MODE_BITSIZE (shift_mode)))
  7710.     {
  7711.       count = 0;
  7712.       break;
  7713.     }
  7714.  
  7715.       /* If we are doing an arithmetic right shift and discarding all but
  7716.      the sign bit copies, this is equivalent to doing a shift by the
  7717.      bitsize minus one.  Convert it into that shift because it will often
  7718.      allow other simplifications.  */
  7719.  
  7720.       if (code == ASHIFTRT
  7721.       && (count + num_sign_bit_copies (varop, shift_mode)
  7722.           >= GET_MODE_BITSIZE (shift_mode)))
  7723.     count = GET_MODE_BITSIZE (shift_mode) - 1;
  7724.  
  7725.       /* We simplify the tests below and elsewhere by converting
  7726.      ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
  7727.      `make_compound_operation' will convert it to a ASHIFTRT for
  7728.      those machines (such as Vax) that don't have a LSHIFTRT.  */
  7729.       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
  7730.       && code == ASHIFTRT
  7731.       && ((nonzero_bits (varop, shift_mode)
  7732.            & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
  7733.           == 0))
  7734.     code = LSHIFTRT;
  7735.  
  7736.       switch (GET_CODE (varop))
  7737.     {
  7738.     case SIGN_EXTEND:
  7739.     case ZERO_EXTEND:
  7740.     case SIGN_EXTRACT:
  7741.     case ZERO_EXTRACT:
  7742.       new = expand_compound_operation (varop);
  7743.       if (new != varop)
  7744.         {
  7745.           varop = new;
  7746.           continue;
  7747.         }
  7748.       break;
  7749.  
  7750.     case MEM:
  7751.       /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
  7752.          minus the width of a smaller mode, we can do this with a
  7753.          SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
  7754.       if ((code == ASHIFTRT || code == LSHIFTRT)
  7755.           && ! mode_dependent_address_p (XEXP (varop, 0))
  7756.           && ! MEM_VOLATILE_P (varop)
  7757.           && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
  7758.                      MODE_INT, 1)) != BLKmode)
  7759.         {
  7760.           if (BYTES_BIG_ENDIAN)
  7761.         new = gen_rtx (MEM, tmode, XEXP (varop, 0));
  7762.           else
  7763.         new = gen_rtx (MEM, tmode,
  7764.                    plus_constant (XEXP (varop, 0),
  7765.                           count / BITS_PER_UNIT));
  7766.           RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
  7767.           MEM_VOLATILE_P (new) = MEM_VOLATILE_P (varop);
  7768.           MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (varop);
  7769.           varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
  7770.                        : ZERO_EXTEND, mode, new);
  7771.           count = 0;
  7772.           continue;
  7773.         }
  7774.       break;
  7775.  
  7776.     case USE:
  7777.       /* Similar to the case above, except that we can only do this if
  7778.          the resulting mode is the same as that of the underlying
  7779.          MEM and adjust the address depending on the *bits* endianness
  7780.          because of the way that bit-field extract insns are defined.  */
  7781.       if ((code == ASHIFTRT || code == LSHIFTRT)
  7782.           && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
  7783.                      MODE_INT, 1)) != BLKmode
  7784.           && tmode == GET_MODE (XEXP (varop, 0)))
  7785.         {
  7786.           if (BITS_BIG_ENDIAN)
  7787.         new = XEXP (varop, 0);
  7788.           else
  7789.         {
  7790.           new = copy_rtx (XEXP (varop, 0));
  7791.           SUBST (XEXP (new, 0), 
  7792.              plus_constant (XEXP (new, 0),
  7793.                     count / BITS_PER_UNIT));
  7794.         }
  7795.  
  7796.           varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
  7797.                        : ZERO_EXTEND, mode, new);
  7798.           count = 0;
  7799.           continue;
  7800.         }
  7801.       break;
  7802.  
  7803.     case SUBREG:
  7804.       /* If VAROP is a SUBREG, strip it as long as the inner operand has
  7805.          the same number of words as what we've seen so far.  Then store
  7806.          the widest mode in MODE.  */
  7807.       if (subreg_lowpart_p (varop)
  7808.           && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
  7809.           > GET_MODE_SIZE (GET_MODE (varop)))
  7810.           && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
  7811.             + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
  7812.           == mode_words))
  7813.         {
  7814.           varop = SUBREG_REG (varop);
  7815.           if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
  7816.         mode = GET_MODE (varop);
  7817.           continue;
  7818.         }
  7819.       break;
  7820.  
  7821.     case MULT:
  7822.       /* Some machines use MULT instead of ASHIFT because MULT
  7823.          is cheaper.  But it is still better on those machines to
  7824.          merge two shifts into one.  */
  7825.       if (GET_CODE (XEXP (varop, 1)) == CONST_INT
  7826.           && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
  7827.         {
  7828.           varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
  7829.                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));;
  7830.           continue;
  7831.         }
  7832.       break;
  7833.  
  7834.     case UDIV:
  7835.       /* Similar, for when divides are cheaper.  */
  7836.       if (GET_CODE (XEXP (varop, 1)) == CONST_INT
  7837.           && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
  7838.         {
  7839.           varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
  7840.                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
  7841.           continue;
  7842.         }
  7843.       break;
  7844.  
  7845.     case ASHIFTRT:
  7846.       /* If we are extracting just the sign bit of an arithmetic right 
  7847.          shift, that shift is not needed.  */
  7848.       if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
  7849.         {
  7850.           varop = XEXP (varop, 0);
  7851.           continue;
  7852.         }
  7853.  
  7854.       /* ... fall through ... */
  7855.  
  7856.     case LSHIFTRT:
  7857.     case ASHIFT:
  7858.     case ROTATE:
  7859.       /* Here we have two nested shifts.  The result is usually the
  7860.          AND of a new shift with a mask.  We compute the result below.  */
  7861.       if (GET_CODE (XEXP (varop, 1)) == CONST_INT
  7862.           && INTVAL (XEXP (varop, 1)) >= 0
  7863.           && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
  7864.           && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
  7865.           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
  7866.         {
  7867.           enum rtx_code first_code = GET_CODE (varop);
  7868.           int first_count = INTVAL (XEXP (varop, 1));
  7869.           unsigned HOST_WIDE_INT mask;
  7870.           rtx mask_rtx;
  7871.  
  7872.           /* We have one common special case.  We can't do any merging if
  7873.          the inner code is an ASHIFTRT of a smaller mode.  However, if
  7874.          we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
  7875.          with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
  7876.          we can convert it to
  7877.          (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
  7878.          This simplifies certain SIGN_EXTEND operations.  */
  7879.           if (code == ASHIFT && first_code == ASHIFTRT
  7880.           && (GET_MODE_BITSIZE (result_mode)
  7881.               - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
  7882.         {
  7883.           /* C3 has the low-order C1 bits zero.  */
  7884.           
  7885.           mask = (GET_MODE_MASK (mode)
  7886.               & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
  7887.  
  7888.           varop = simplify_and_const_int (NULL_RTX, result_mode,
  7889.                           XEXP (varop, 0), mask);
  7890.           varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
  7891.                         varop, count);
  7892.           count = first_count;
  7893.           code = ASHIFTRT;
  7894.           continue;
  7895.         }
  7896.           
  7897.           /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
  7898.          than C1 high-order bits equal to the sign bit, we can convert
  7899.          this to either an ASHIFT or a ASHIFTRT depending on the
  7900.          two counts. 
  7901.  
  7902.          We cannot do this if VAROP's mode is not SHIFT_MODE.  */
  7903.  
  7904.           if (code == ASHIFTRT && first_code == ASHIFT
  7905.           && GET_MODE (varop) == shift_mode
  7906.           && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
  7907.               > first_count))
  7908.         {
  7909.           count -= first_count;
  7910.           if (count < 0)
  7911.             count = - count, code = ASHIFT;
  7912.           varop = XEXP (varop, 0);
  7913.           continue;
  7914.         }
  7915.  
  7916.           /* There are some cases we can't do.  If CODE is ASHIFTRT,
  7917.          we can only do this if FIRST_CODE is also ASHIFTRT.
  7918.  
  7919.          We can't do the case when CODE is ROTATE and FIRST_CODE is
  7920.          ASHIFTRT.
  7921.  
  7922.          If the mode of this shift is not the mode of the outer shift,
  7923.          we can't do this if either shift is a right shift or ROTATE.
  7924.  
  7925.          Finally, we can't do any of these if the mode is too wide
  7926.          unless the codes are the same.
  7927.  
  7928.          Handle the case where the shift codes are the same
  7929.          first.  */
  7930.  
  7931.           if (code == first_code)
  7932.         {
  7933.           if (GET_MODE (varop) != result_mode
  7934.               && (code == ASHIFTRT || code == LSHIFTRT
  7935.               || code == ROTATE))
  7936.             break;
  7937.  
  7938.           count += first_count;
  7939.           varop = XEXP (varop, 0);
  7940.           continue;
  7941.         }
  7942.  
  7943.           if (code == ASHIFTRT
  7944.           || (code == ROTATE && first_code == ASHIFTRT)
  7945.           || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
  7946.           || (GET_MODE (varop) != result_mode
  7947.               && (first_code == ASHIFTRT || first_code == LSHIFTRT
  7948.               || first_code == ROTATE
  7949.               || code == ROTATE)))
  7950.         break;
  7951.  
  7952.           /* To compute the mask to apply after the shift, shift the
  7953.          nonzero bits of the inner shift the same way the 
  7954.          outer shift will.  */
  7955.  
  7956.           mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
  7957.  
  7958.           mask_rtx
  7959.         = simplify_binary_operation (code, result_mode, mask_rtx,
  7960.                          GEN_INT (count));
  7961.                   
  7962.           /* Give up if we can't compute an outer operation to use.  */
  7963.           if (mask_rtx == 0
  7964.           || GET_CODE (mask_rtx) != CONST_INT
  7965.           || ! merge_outer_ops (&outer_op, &outer_const, AND,
  7966.                     INTVAL (mask_rtx),
  7967.                     result_mode, &complement_p))
  7968.         break;
  7969.  
  7970.           /* If the shifts are in the same direction, we add the
  7971.          counts.  Otherwise, we subtract them.  */
  7972.           if ((code == ASHIFTRT || code == LSHIFTRT)
  7973.           == (first_code == ASHIFTRT || first_code == LSHIFTRT))
  7974.         count += first_count;
  7975.           else
  7976.         count -= first_count;
  7977.  
  7978.           /* If COUNT is positive, the new shift is usually CODE, 
  7979.          except for the two exceptions below, in which case it is
  7980.          FIRST_CODE.  If the count is negative, FIRST_CODE should
  7981.          always be used  */
  7982.           if (count > 0
  7983.           && ((first_code == ROTATE && code == ASHIFT)
  7984.               || (first_code == ASHIFTRT && code == LSHIFTRT)))
  7985.         code = first_code;
  7986.           else if (count < 0)
  7987.         code = first_code, count = - count;
  7988.  
  7989.           varop = XEXP (varop, 0);
  7990.           continue;
  7991.         }
  7992.  
  7993.       /* If we have (A << B << C) for any shift, we can convert this to
  7994.          (A << C << B).  This wins if A is a constant.  Only try this if
  7995.          B is not a constant.  */
  7996.  
  7997.       else if (GET_CODE (varop) == code
  7998.            && GET_CODE (XEXP (varop, 1)) != CONST_INT
  7999.            && 0 != (new
  8000.                 = simplify_binary_operation (code, mode,
  8001.                              XEXP (varop, 0),
  8002.                              GEN_INT (count))))
  8003.         {
  8004.           varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
  8005.           count = 0;
  8006.           continue;
  8007.         }
  8008.       break;
  8009.  
  8010.     case NOT:
  8011.       /* Make this fit the case below.  */
  8012.       varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
  8013.                    GEN_INT (GET_MODE_MASK (mode)));
  8014.       continue;
  8015.  
  8016.     case IOR:
  8017.     case AND:
  8018.     case XOR:
  8019.       /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
  8020.          with C the size of VAROP - 1 and the shift is logical if
  8021.          STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
  8022.          we have an (le X 0) operation.   If we have an arithmetic shift
  8023.          and STORE_FLAG_VALUE is 1 or we have a logical shift with
  8024.          STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
  8025.  
  8026.       if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
  8027.           && XEXP (XEXP (varop, 0), 1) == constm1_rtx
  8028.           && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  8029.           && (code == LSHIFTRT || code == ASHIFTRT)
  8030.           && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
  8031.           && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
  8032.         {
  8033.           count = 0;
  8034.           varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
  8035.                        const0_rtx);
  8036.  
  8037.           if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
  8038.         varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
  8039.  
  8040.           continue;
  8041.         }
  8042.  
  8043.       /* If we have (shift (logical)), move the logical to the outside
  8044.          to allow it to possibly combine with another logical and the
  8045.          shift to combine with another shift.  This also canonicalizes to
  8046.          what a ZERO_EXTRACT looks like.  Also, some machines have
  8047.          (and (shift)) insns.  */
  8048.  
  8049.       if (GET_CODE (XEXP (varop, 1)) == CONST_INT
  8050.           && (new = simplify_binary_operation (code, result_mode,
  8051.                            XEXP (varop, 1),
  8052.                            GEN_INT (count))) != 0
  8053.           && GET_CODE(new) == CONST_INT
  8054.           && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
  8055.                   INTVAL (new), result_mode, &complement_p))
  8056.         {
  8057.           varop = XEXP (varop, 0);
  8058.           continue;
  8059.         }
  8060.  
  8061.       /* If we can't do that, try to simplify the shift in each arm of the
  8062.          logical expression, make a new logical expression, and apply
  8063.          the inverse distributive law.  */
  8064.       {
  8065.         rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
  8066.                         XEXP (varop, 0), count);
  8067.         rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
  8068.                         XEXP (varop, 1), count);
  8069.  
  8070.         varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
  8071.         varop = apply_distributive_law (varop);
  8072.  
  8073.         count = 0;
  8074.       }
  8075.       break;
  8076.  
  8077.     case EQ:
  8078.       /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
  8079.          says that the sign bit can be tested, FOO has mode MODE, C is
  8080.          GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
  8081.          that may be nonzero.  */
  8082.       if (code == LSHIFTRT
  8083.           && XEXP (varop, 1) == const0_rtx
  8084.           && GET_MODE (XEXP (varop, 0)) == result_mode
  8085.           && count == GET_MODE_BITSIZE (result_mode) - 1
  8086.           && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
  8087.           && ((STORE_FLAG_VALUE
  8088.            & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
  8089.           && nonzero_bits (XEXP (varop, 0), result_mode) == 1
  8090.           && merge_outer_ops (&outer_op, &outer_const, XOR,
  8091.                   (HOST_WIDE_INT) 1, result_mode,
  8092.                   &complement_p))
  8093.         {
  8094.           varop = XEXP (varop, 0);
  8095.           count = 0;
  8096.           continue;
  8097.         }
  8098.       break;
  8099.  
  8100.     case NEG:
  8101.       /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
  8102.          than the number of bits in the mode is equivalent to A.  */
  8103.       if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
  8104.           && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
  8105.         {
  8106.           varop = XEXP (varop, 0);
  8107.           count = 0;
  8108.           continue;
  8109.         }
  8110.  
  8111.       /* NEG commutes with ASHIFT since it is multiplication.  Move the
  8112.          NEG outside to allow shifts to combine.  */
  8113.       if (code == ASHIFT
  8114.           && merge_outer_ops (&outer_op, &outer_const, NEG,
  8115.                   (HOST_WIDE_INT) 0, result_mode,
  8116.                   &complement_p))
  8117.         {
  8118.           varop = XEXP (varop, 0);
  8119.           continue;
  8120.         }
  8121.       break;
  8122.  
  8123.     case PLUS:
  8124.       /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
  8125.          is one less than the number of bits in the mode is
  8126.          equivalent to (xor A 1).  */
  8127.       if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
  8128.           && XEXP (varop, 1) == constm1_rtx
  8129.           && nonzero_bits (XEXP (varop, 0), result_mode) == 1
  8130.           && merge_outer_ops (&outer_op, &outer_const, XOR,
  8131.                   (HOST_WIDE_INT) 1, result_mode,
  8132.                   &complement_p))
  8133.         {
  8134.           count = 0;
  8135.           varop = XEXP (varop, 0);
  8136.           continue;
  8137.         }
  8138.  
  8139.       /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
  8140.          that might be nonzero in BAR are those being shifted out and those
  8141.          bits are known zero in FOO, we can replace the PLUS with FOO.
  8142.          Similarly in the other operand order.  This code occurs when
  8143.          we are computing the size of a variable-size array.  */
  8144.  
  8145.       if ((code == ASHIFTRT || code == LSHIFTRT)
  8146.           && count < HOST_BITS_PER_WIDE_INT
  8147.           && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
  8148.           && (nonzero_bits (XEXP (varop, 1), result_mode)
  8149.           & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
  8150.         {
  8151.           varop = XEXP (varop, 0);
  8152.           continue;
  8153.         }
  8154.       else if ((code == ASHIFTRT || code == LSHIFTRT)
  8155.            && count < HOST_BITS_PER_WIDE_INT
  8156.            && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
  8157.            && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
  8158.                 >> count)
  8159.            && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
  8160.                 & nonzero_bits (XEXP (varop, 1),
  8161.                          result_mode)))
  8162.         {
  8163.           varop = XEXP (varop, 1);
  8164.           continue;
  8165.         }
  8166.  
  8167.       /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
  8168.       if (code == ASHIFT
  8169.           && GET_CODE (XEXP (varop, 1)) == CONST_INT
  8170.           && (new = simplify_binary_operation (ASHIFT, result_mode,
  8171.                            XEXP (varop, 1),
  8172.                            GEN_INT (count))) != 0
  8173.           && GET_CODE(new) == CONST_INT
  8174.           && merge_outer_ops (&outer_op, &outer_const, PLUS,
  8175.                   INTVAL (new), result_mode, &complement_p))
  8176.         {
  8177.           varop = XEXP (varop, 0);
  8178.           continue;
  8179.         }
  8180.       break;
  8181.  
  8182.     case MINUS:
  8183.       /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
  8184.          with C the size of VAROP - 1 and the shift is logical if
  8185.          STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
  8186.          we have a (gt X 0) operation.  If the shift is arithmetic with
  8187.          STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
  8188.          we have a (neg (gt X 0)) operation.  */
  8189.  
  8190.       if (GET_CODE (XEXP (varop, 0)) == ASHIFTRT
  8191.           && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
  8192.           && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  8193.           && (code == LSHIFTRT || code == ASHIFTRT)
  8194.           && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
  8195.           && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
  8196.           && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
  8197.         {
  8198.           count = 0;
  8199.           varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
  8200.                        const0_rtx);
  8201.  
  8202.           if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
  8203.         varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
  8204.  
  8205.           continue;
  8206.         }
  8207.       break;
  8208.     }
  8209.  
  8210.       break;
  8211.     }
  8212.  
  8213.   /* We need to determine what mode to do the shift in.  If the shift is
  8214.      a right shift or ROTATE, we must always do it in the mode it was
  8215.      originally done in.  Otherwise, we can do it in MODE, the widest mode
  8216.      encountered.  The code we care about is that of the shift that will
  8217.      actually be done, not the shift that was originally requested.  */
  8218.   shift_mode
  8219.     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
  8220.        ? result_mode : mode);
  8221.  
  8222.   /* We have now finished analyzing the shift.  The result should be
  8223.      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
  8224.      OUTER_OP is non-NIL, it is an operation that needs to be applied
  8225.      to the result of the shift.  OUTER_CONST is the relevant constant,
  8226.      but we must turn off all bits turned off in the shift.
  8227.  
  8228.      If we were passed a value for X, see if we can use any pieces of
  8229.      it.  If not, make new rtx.  */
  8230.  
  8231.   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
  8232.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  8233.       && INTVAL (XEXP (x, 1)) == count)
  8234.     const_rtx = XEXP (x, 1);
  8235.   else
  8236.     const_rtx = GEN_INT (count);
  8237.  
  8238.   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
  8239.       && GET_MODE (XEXP (x, 0)) == shift_mode
  8240.       && SUBREG_REG (XEXP (x, 0)) == varop)
  8241.     varop = XEXP (x, 0);
  8242.   else if (GET_MODE (varop) != shift_mode)
  8243.     varop = gen_lowpart_for_combine (shift_mode, varop);
  8244.  
  8245.   /* If we can't make the SUBREG, try to return what we were given. */
  8246.   if (GET_CODE (varop) == CLOBBER)
  8247.     return x ? x : varop;
  8248.  
  8249.   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
  8250.   if (new != 0)
  8251.     x = new;
  8252.   else
  8253.     {
  8254.       if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
  8255.     x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
  8256.  
  8257.       SUBST (XEXP (x, 0), varop);
  8258.       SUBST (XEXP (x, 1), const_rtx);
  8259.     }
  8260.  
  8261.   /* If we have an outer operation and we just made a shift, it is
  8262.      possible that we could have simplified the shift were it not
  8263.      for the outer operation.  So try to do the simplification
  8264.      recursively.  */
  8265.  
  8266.   if (outer_op != NIL && GET_CODE (x) == code
  8267.       && GET_CODE (XEXP (x, 1)) == CONST_INT)
  8268.     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
  8269.                   INTVAL (XEXP (x, 1)));
  8270.  
  8271.   /* If we were doing a LSHIFTRT in a wider mode than it was originally,
  8272.      turn off all the bits that the shift would have turned off.  */
  8273.   if (orig_code == LSHIFTRT && result_mode != shift_mode)
  8274.     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
  8275.                 GET_MODE_MASK (result_mode) >> orig_count);
  8276.       
  8277.   /* Do the remainder of the processing in RESULT_MODE.  */
  8278.   x = gen_lowpart_for_combine (result_mode, x);
  8279.  
  8280.   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
  8281.      operation.  */
  8282.   if (complement_p)
  8283.     x = gen_unary (NOT, result_mode, result_mode, x);
  8284.  
  8285.   if (outer_op != NIL)
  8286.     {
  8287.       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
  8288.     {
  8289.       int width = GET_MODE_BITSIZE (result_mode);
  8290.  
  8291.       outer_const &= GET_MODE_MASK (result_mode);
  8292.  
  8293.       /* If this would be an entire word for the target, but is not for
  8294.          the host, then sign-extend on the host so that the number will
  8295.          look the same way on the host that it would on the target.
  8296.  
  8297.          For example, when building a 64 bit alpha hosted 32 bit sparc
  8298.          targeted compiler, then we want the 32 bit unsigned value -1 to be
  8299.          represented as a 64 bit value -1, and not as 0x00000000ffffffff.
  8300.          The later confuses the sparc backend.  */
  8301.  
  8302.       if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
  8303.           && (outer_const & ((HOST_WIDE_INT) 1 << (width - 1))))
  8304.         outer_const |= ((HOST_WIDE_INT) (-1) << width);
  8305.     }
  8306.  
  8307.       if (outer_op == AND)
  8308.     x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
  8309.       else if (outer_op == SET)
  8310.     /* This means that we have determined that the result is
  8311.        equivalent to a constant.  This should be rare.  */
  8312.     x = GEN_INT (outer_const);
  8313.       else if (GET_RTX_CLASS (outer_op) == '1')
  8314.     x = gen_unary (outer_op, result_mode, result_mode, x);
  8315.       else
  8316.     x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
  8317.     }
  8318.  
  8319.   return x;
  8320. }  
  8321.  
  8322. /* Like recog, but we receive the address of a pointer to a new pattern.
  8323.    We try to match the rtx that the pointer points to.
  8324.    If that fails, we may try to modify or replace the pattern,
  8325.    storing the replacement into the same pointer object.
  8326.  
  8327.    Modifications include deletion or addition of CLOBBERs.
  8328.  
  8329.    PNOTES is a pointer to a location where any REG_UNUSED notes added for
  8330.    the CLOBBERs are placed.
  8331.  
  8332.    PADDED_SCRATCHES is set to the number of (clobber (scratch)) patterns
  8333.    we had to add.
  8334.  
  8335.    The value is the final insn code from the pattern ultimately matched,
  8336.    or -1.  */
  8337.  
  8338. static int
  8339. recog_for_combine (pnewpat, insn, pnotes, padded_scratches)
  8340.      rtx *pnewpat;
  8341.      rtx insn;
  8342.      rtx *pnotes;
  8343.      int *padded_scratches;
  8344. {
  8345.   register rtx pat = *pnewpat;
  8346.   int insn_code_number;
  8347.   int num_clobbers_to_add = 0;
  8348.   int i;
  8349.   rtx notes = 0;
  8350.  
  8351.   *padded_scratches = 0;
  8352.  
  8353.   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
  8354.      we use to indicate that something didn't match.  If we find such a
  8355.      thing, force rejection.  */
  8356.   if (GET_CODE (pat) == PARALLEL)
  8357.     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
  8358.       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
  8359.       && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
  8360.     return -1;
  8361.  
  8362.   /* Is the result of combination a valid instruction?  */
  8363.   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
  8364.  
  8365.   /* If it isn't, there is the possibility that we previously had an insn
  8366.      that clobbered some register as a side effect, but the combined
  8367.      insn doesn't need to do that.  So try once more without the clobbers
  8368.      unless this represents an ASM insn.  */
  8369.  
  8370.   if (insn_code_number < 0 && ! check_asm_operands (pat)
  8371.       && GET_CODE (pat) == PARALLEL)
  8372.     {
  8373.       int pos;
  8374.  
  8375.       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
  8376.     if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
  8377.       {
  8378.         if (i != pos)
  8379.           SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
  8380.         pos++;
  8381.       }
  8382.  
  8383.       SUBST_INT (XVECLEN (pat, 0), pos);
  8384.  
  8385.       if (pos == 1)
  8386.     pat = XVECEXP (pat, 0, 0);
  8387.  
  8388.       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
  8389.     }
  8390.  
  8391.   /* If we had any clobbers to add, make a new pattern than contains
  8392.      them.  Then check to make sure that all of them are dead.  */
  8393.   if (num_clobbers_to_add)
  8394.     {
  8395.       rtx newpat = gen_rtx (PARALLEL, VOIDmode,
  8396.                 gen_rtvec (GET_CODE (pat) == PARALLEL
  8397.                        ? XVECLEN (pat, 0) + num_clobbers_to_add
  8398.                        : num_clobbers_to_add + 1));
  8399.  
  8400.       if (GET_CODE (pat) == PARALLEL)
  8401.     for (i = 0; i < XVECLEN (pat, 0); i++)
  8402.       XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
  8403.       else
  8404.     XVECEXP (newpat, 0, 0) = pat;
  8405.  
  8406.       add_clobbers (newpat, insn_code_number);
  8407.  
  8408.       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
  8409.        i < XVECLEN (newpat, 0); i++)
  8410.     {
  8411.       if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
  8412.           && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
  8413.         return -1;
  8414.       else if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == SCRATCH)
  8415.         (*padded_scratches)++;
  8416.       notes = gen_rtx (EXPR_LIST, REG_UNUSED,
  8417.                XEXP (XVECEXP (newpat, 0, i), 0), notes);
  8418.     }
  8419.       pat = newpat;
  8420.     }
  8421.  
  8422.   *pnewpat = pat;
  8423.   *pnotes = notes;
  8424.  
  8425.   return insn_code_number;
  8426. }
  8427.  
  8428. /* Like gen_lowpart but for use by combine.  In combine it is not possible
  8429.    to create any new pseudoregs.  However, it is safe to create
  8430.    invalid memory addresses, because combine will try to recognize
  8431.    them and all they will do is make the combine attempt fail.
  8432.  
  8433.    If for some reason this cannot do its job, an rtx
  8434.    (clobber (const_int 0)) is returned.
  8435.    An insn containing that will not be recognized.  */
  8436.  
  8437. #undef gen_lowpart
  8438.  
  8439. static rtx
  8440. gen_lowpart_for_combine (mode, x)
  8441.      enum machine_mode mode;
  8442.      register rtx x;
  8443. {
  8444.   rtx result;
  8445.  
  8446.   if (GET_MODE (x) == mode)
  8447.     return x;
  8448.  
  8449.   /* We can only support MODE being wider than a word if X is a
  8450.      constant integer or has a mode the same size.  */
  8451.  
  8452.   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
  8453.       && ! ((GET_MODE (x) == VOIDmode
  8454.          && (GET_CODE (x) == CONST_INT
  8455.          || GET_CODE (x) == CONST_DOUBLE))
  8456.         || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
  8457.     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
  8458.  
  8459.   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
  8460.      won't know what to do.  So we will strip off the SUBREG here and
  8461.      process normally.  */
  8462.   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
  8463.     {
  8464.       x = SUBREG_REG (x);
  8465.       if (GET_MODE (x) == mode)
  8466.     return x;
  8467.     }
  8468.  
  8469.   result = gen_lowpart_common (mode, x);
  8470.   if (result != 0
  8471.       && GET_CODE (result) == SUBREG
  8472.       && GET_CODE (SUBREG_REG (result)) == REG
  8473.       && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
  8474.       && (GET_MODE_SIZE (GET_MODE (result))
  8475.       != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
  8476.     reg_changes_size[REGNO (SUBREG_REG (result))] = 1;
  8477.  
  8478.   if (result)
  8479.     return result;
  8480.  
  8481.   if (GET_CODE (x) == MEM)
  8482.     {
  8483.       register int offset = 0;
  8484.       rtx new;
  8485.  
  8486.       /* Refuse to work on a volatile memory ref or one with a mode-dependent
  8487.      address.  */
  8488.       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
  8489.     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
  8490.  
  8491.       /* If we want to refer to something bigger than the original memref,
  8492.      generate a perverse subreg instead.  That will force a reload
  8493.      of the original memref X.  */
  8494.       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
  8495.     return gen_rtx (SUBREG, mode, x, 0);
  8496.  
  8497.       if (WORDS_BIG_ENDIAN)
  8498.     offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
  8499.           - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
  8500.       if (BYTES_BIG_ENDIAN)
  8501.     {
  8502.       /* Adjust the address so that the address-after-the-data is
  8503.          unchanged.  */
  8504.       offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
  8505.              - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
  8506.     }
  8507.       new = gen_rtx (MEM, mode, plus_constant (XEXP (x, 0), offset));
  8508.       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
  8509.       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
  8510.       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
  8511.       return new;
  8512.     }
  8513.  
  8514.   /* If X is a comparison operator, rewrite it in a new mode.  This
  8515.      probably won't match, but may allow further simplifications.  */
  8516.   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
  8517.     return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
  8518.  
  8519.   /* If we couldn't simplify X any other way, just enclose it in a
  8520.      SUBREG.  Normally, this SUBREG won't match, but some patterns may
  8521.      include an explicit SUBREG or we may simplify it further in combine.  */
  8522.   else
  8523.     {
  8524.       int word = 0;
  8525.  
  8526.       if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
  8527.     word = ((GET_MODE_SIZE (GET_MODE (x))
  8528.          - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
  8529.         / UNITS_PER_WORD);
  8530.       return gen_rtx (SUBREG, mode, x, word);
  8531.     }
  8532. }
  8533.  
  8534. /* Make an rtx expression.  This is a subset of gen_rtx and only supports
  8535.    expressions of 1, 2, or 3 operands, each of which are rtx expressions.
  8536.  
  8537.    If the identical expression was previously in the insn (in the undobuf),
  8538.    it will be returned.  Only if it is not found will a new expression
  8539.    be made.  */
  8540.  
  8541. /*VARARGS2*/
  8542. static rtx
  8543. gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
  8544. {
  8545. #ifndef __STDC__
  8546.   enum rtx_code code;
  8547.   enum machine_mode mode;
  8548. #endif
  8549.   va_list p;
  8550.   int n_args;
  8551.   rtx args[3];
  8552.   int i, j;
  8553.   char *fmt;
  8554.   rtx rt;
  8555.  
  8556.   VA_START (p, mode);
  8557.  
  8558. #ifndef __STDC__
  8559.   code = va_arg (p, enum rtx_code);
  8560.   mode = va_arg (p, enum machine_mode);
  8561. #endif
  8562.  
  8563.   n_args = GET_RTX_LENGTH (code);
  8564.   fmt = GET_RTX_FORMAT (code);
  8565.  
  8566.   if (n_args == 0 || n_args > 3)
  8567.     abort ();
  8568.  
  8569.   /* Get each arg and verify that it is supposed to be an expression.  */
  8570.   for (j = 0; j < n_args; j++)
  8571.     {
  8572.       if (*fmt++ != 'e')
  8573.     abort ();
  8574.  
  8575.       args[j] = va_arg (p, rtx);
  8576.     }
  8577.  
  8578.   /* See if this is in undobuf.  Be sure we don't use objects that came
  8579.      from another insn; this could produce circular rtl structures.  */
  8580.  
  8581.   for (i = previous_num_undos; i < undobuf.num_undo; i++)
  8582.     if (!undobuf.undo[i].is_int
  8583.     && GET_CODE (undobuf.undo[i].old_contents.r) == code
  8584.     && GET_MODE (undobuf.undo[i].old_contents.r) == mode)
  8585.       {
  8586.     for (j = 0; j < n_args; j++)
  8587.       if (XEXP (undobuf.undo[i].old_contents.r, j) != args[j])
  8588.         break;
  8589.  
  8590.     if (j == n_args)
  8591.       return undobuf.undo[i].old_contents.r;
  8592.       }
  8593.  
  8594.   /* Otherwise make a new rtx.  We know we have 1, 2, or 3 args.
  8595.      Use rtx_alloc instead of gen_rtx because it's faster on RISC.  */
  8596.   rt = rtx_alloc (code);
  8597.   PUT_MODE (rt, mode);
  8598.   XEXP (rt, 0) = args[0];
  8599.   if (n_args > 1)
  8600.     {
  8601.       XEXP (rt, 1) = args[1];
  8602.       if (n_args > 2)
  8603.     XEXP (rt, 2) = args[2];
  8604.     }
  8605.   return rt;
  8606. }
  8607.  
  8608. /* These routines make binary and unary operations by first seeing if they
  8609.    fold; if not, a new expression is allocated.  */
  8610.  
  8611. static rtx
  8612. gen_binary (code, mode, op0, op1)
  8613.      enum rtx_code code;
  8614.      enum machine_mode mode;
  8615.      rtx op0, op1;
  8616. {
  8617.   rtx result;
  8618.   rtx tem;
  8619.  
  8620.   if (GET_RTX_CLASS (code) == 'c'
  8621.       && (GET_CODE (op0) == CONST_INT
  8622.       || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
  8623.     tem = op0, op0 = op1, op1 = tem;
  8624.  
  8625.   if (GET_RTX_CLASS (code) == '<') 
  8626.     {
  8627.       enum machine_mode op_mode = GET_MODE (op0);
  8628.  
  8629.       /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get 
  8630.      just (REL_OP X Y). */
  8631.       if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
  8632.     {
  8633.       op1 = XEXP (op0, 1);
  8634.       op0 = XEXP (op0, 0);
  8635.       op_mode = GET_MODE (op0);
  8636.     }
  8637.  
  8638.       if (op_mode == VOIDmode)
  8639.     op_mode = GET_MODE (op1);
  8640.       result = simplify_relational_operation (code, op_mode, op0, op1);
  8641.     }
  8642.   else
  8643.     result = simplify_binary_operation (code, mode, op0, op1);
  8644.  
  8645.   if (result)
  8646.     return result;
  8647.  
  8648.   /* Put complex operands first and constants second.  */
  8649.   if (GET_RTX_CLASS (code) == 'c'
  8650.       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
  8651.       || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
  8652.           && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
  8653.       || (GET_CODE (op0) == SUBREG
  8654.           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
  8655.           && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
  8656.     return gen_rtx_combine (code, mode, op1, op0);
  8657.  
  8658.   return gen_rtx_combine (code, mode, op0, op1);
  8659. }
  8660.  
  8661. static rtx
  8662. gen_unary (code, mode, op0_mode, op0)
  8663.      enum rtx_code code;
  8664.      enum machine_mode mode, op0_mode;
  8665.      rtx op0;
  8666. {
  8667.   rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
  8668.  
  8669.   if (result)
  8670.     return result;
  8671.  
  8672.   return gen_rtx_combine (code, mode, op0);
  8673. }
  8674.  
  8675. /* Simplify a comparison between *POP0 and *POP1 where CODE is the
  8676.    comparison code that will be tested.
  8677.  
  8678.    The result is a possibly different comparison code to use.  *POP0 and
  8679.    *POP1 may be updated.
  8680.  
  8681.    It is possible that we might detect that a comparison is either always
  8682.    true or always false.  However, we do not perform general constant
  8683.    folding in combine, so this knowledge isn't useful.  Such tautologies
  8684.    should have been detected earlier.  Hence we ignore all such cases.  */
  8685.  
  8686. static enum rtx_code
  8687. simplify_comparison (code, pop0, pop1)
  8688.      enum rtx_code code;
  8689.      rtx *pop0;
  8690.      rtx *pop1;
  8691. {
  8692.   rtx op0 = *pop0;
  8693.   rtx op1 = *pop1;
  8694.   rtx tem, tem1;
  8695.   int i;
  8696.   enum machine_mode mode, tmode;
  8697.  
  8698.   /* Try a few ways of applying the same transformation to both operands.  */
  8699.   while (1)
  8700.     {
  8701. #ifndef WORD_REGISTER_OPERATIONS
  8702.       /* The test below this one won't handle SIGN_EXTENDs on these machines,
  8703.      so check specially.  */
  8704.       if (code != GTU && code != GEU && code != LTU && code != LEU
  8705.       && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
  8706.       && GET_CODE (XEXP (op0, 0)) == ASHIFT
  8707.       && GET_CODE (XEXP (op1, 0)) == ASHIFT
  8708.       && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
  8709.       && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
  8710.       && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
  8711.           == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
  8712.       && GET_CODE (XEXP (op0, 1)) == CONST_INT
  8713.       && GET_CODE (XEXP (op1, 1)) == CONST_INT
  8714.       && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
  8715.       && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
  8716.       && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
  8717.       && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
  8718.       && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
  8719.       && (INTVAL (XEXP (op0, 1))
  8720.           == (GET_MODE_BITSIZE (GET_MODE (op0))
  8721.           - (GET_MODE_BITSIZE
  8722.              (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
  8723.     {
  8724.       op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
  8725.       op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
  8726.     }
  8727. #endif
  8728.  
  8729.       /* If both operands are the same constant shift, see if we can ignore the
  8730.      shift.  We can if the shift is a rotate or if the bits shifted out of
  8731.      this shift are known to be zero for both inputs and if the type of
  8732.      comparison is compatible with the shift.  */
  8733.       if (GET_CODE (op0) == GET_CODE (op1)
  8734.       && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
  8735.       && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
  8736.           || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
  8737.           && (code != GT && code != LT && code != GE && code != LE))
  8738.           || (GET_CODE (op0) == ASHIFTRT
  8739.           && (code != GTU && code != LTU
  8740.               && code != GEU && code != GEU)))
  8741.       && GET_CODE (XEXP (op0, 1)) == CONST_INT
  8742.       && INTVAL (XEXP (op0, 1)) >= 0
  8743.       && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
  8744.       && XEXP (op0, 1) == XEXP (op1, 1))
  8745.     {
  8746.       enum machine_mode mode = GET_MODE (op0);
  8747.       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
  8748.       int shift_count = INTVAL (XEXP (op0, 1));
  8749.  
  8750.       if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
  8751.         mask &= (mask >> shift_count) << shift_count;
  8752.       else if (GET_CODE (op0) == ASHIFT)
  8753.         mask = (mask & (mask << shift_count)) >> shift_count;
  8754.  
  8755.       if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
  8756.           && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
  8757.         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
  8758.       else
  8759.         break;
  8760.     }
  8761.  
  8762.       /* If both operands are AND's of a paradoxical SUBREG by constant, the
  8763.      SUBREGs are of the same mode, and, in both cases, the AND would
  8764.      be redundant if the comparison was done in the narrower mode,
  8765.      do the comparison in the narrower mode (e.g., we are AND'ing with 1
  8766.      and the operand's possibly nonzero bits are 0xffffff01; in that case
  8767.      if we only care about QImode, we don't need the AND).  This case
  8768.      occurs if the output mode of an scc insn is not SImode and
  8769.      STORE_FLAG_VALUE == 1 (e.g., the 386).
  8770.  
  8771.      Similarly, check for a case where the AND's are ZERO_EXTEND
  8772.      operations from some narrower mode even though a SUBREG is not
  8773.      present.  */
  8774.  
  8775.       else if  (GET_CODE (op0) == AND && GET_CODE (op1) == AND
  8776.         && GET_CODE (XEXP (op0, 1)) == CONST_INT
  8777.         && GET_CODE (XEXP (op1, 1)) == CONST_INT)
  8778.     {
  8779.       rtx inner_op0 = XEXP (op0, 0);
  8780.       rtx inner_op1 = XEXP (op1, 0);
  8781.       HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
  8782.       HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
  8783.       int changed = 0;
  8784.         
  8785.       if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
  8786.           && (GET_MODE_SIZE (GET_MODE (inner_op0))
  8787.           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
  8788.           && (GET_MODE (SUBREG_REG (inner_op0))
  8789.           == GET_MODE (SUBREG_REG (inner_op1)))
  8790.           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
  8791.           <= HOST_BITS_PER_WIDE_INT)
  8792.           && (0 == (~c0) & nonzero_bits (SUBREG_REG (inner_op0),
  8793.                          GET_MODE (SUBREG_REG (op0))))
  8794.           && (0 == (~c1) & nonzero_bits (SUBREG_REG (inner_op1),
  8795.                          GET_MODE (SUBREG_REG (inner_op1)))))
  8796.         {
  8797.           op0 = SUBREG_REG (inner_op0);
  8798.           op1 = SUBREG_REG (inner_op1);
  8799.  
  8800.           /* The resulting comparison is always unsigned since we masked
  8801.          off the original sign bit. */
  8802.           code = unsigned_condition (code);
  8803.  
  8804.           changed = 1;
  8805.         }
  8806.  
  8807.       else if (c0 == c1)
  8808.         for (tmode = GET_CLASS_NARROWEST_MODE
  8809.          (GET_MODE_CLASS (GET_MODE (op0)));
  8810.          tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
  8811.           if (c0 == GET_MODE_MASK (tmode))
  8812.         {
  8813.           op0 = gen_lowpart_for_combine (tmode, inner_op0);
  8814.           op1 = gen_lowpart_for_combine (tmode, inner_op1);
  8815.           code = unsigned_condition (code);
  8816.           changed = 1;
  8817.           break;
  8818.         }
  8819.  
  8820.       if (! changed)
  8821.         break;
  8822.     }
  8823.  
  8824.       /* If both operands are NOT, we can strip off the outer operation
  8825.      and adjust the comparison code for swapped operands; similarly for
  8826.      NEG, except that this must be an equality comparison.  */
  8827.       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
  8828.            || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
  8829.            && (code == EQ || code == NE)))
  8830.     op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
  8831.  
  8832.       else
  8833.     break;
  8834.     }
  8835.      
  8836.   /* If the first operand is a constant, swap the operands and adjust the
  8837.      comparison code appropriately.  */
  8838.   if (CONSTANT_P (op0))
  8839.     {
  8840.       tem = op0, op0 = op1, op1 = tem;
  8841.       code = swap_condition (code);
  8842.     }
  8843.  
  8844.   /* We now enter a loop during which we will try to simplify the comparison.
  8845.      For the most part, we only are concerned with comparisons with zero,
  8846.      but some things may really be comparisons with zero but not start
  8847.      out looking that way.  */
  8848.  
  8849.   while (GET_CODE (op1) == CONST_INT)
  8850.     {
  8851.       enum machine_mode mode = GET_MODE (op0);
  8852.       int mode_width = GET_MODE_BITSIZE (mode);
  8853.       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
  8854.       int equality_comparison_p;
  8855.       int sign_bit_comparison_p;
  8856.       int unsigned_comparison_p;
  8857.       HOST_WIDE_INT const_op;
  8858.  
  8859.       /* We only want to handle integral modes.  This catches VOIDmode,
  8860.      CCmode, and the floating-point modes.  An exception is that we
  8861.      can handle VOIDmode if OP0 is a COMPARE or a comparison
  8862.      operation.  */
  8863.  
  8864.       if (GET_MODE_CLASS (mode) != MODE_INT
  8865.       && ! (mode == VOIDmode
  8866.         && (GET_CODE (op0) == COMPARE
  8867.             || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
  8868.     break;
  8869.  
  8870.       /* Get the constant we are comparing against and turn off all bits
  8871.      not on in our mode.  */
  8872.       const_op = INTVAL (op1);
  8873.       if (mode_width <= HOST_BITS_PER_WIDE_INT)
  8874.     const_op &= mask;
  8875.  
  8876.       /* If we are comparing against a constant power of two and the value
  8877.      being compared can only have that single bit nonzero (e.g., it was
  8878.      `and'ed with that bit), we can replace this with a comparison
  8879.      with zero.  */
  8880.       if (const_op
  8881.       && (code == EQ || code == NE || code == GE || code == GEU
  8882.           || code == LT || code == LTU)
  8883.       && mode_width <= HOST_BITS_PER_WIDE_INT
  8884.       && exact_log2 (const_op) >= 0
  8885.       && nonzero_bits (op0, mode) == const_op)
  8886.     {
  8887.       code = (code == EQ || code == GE || code == GEU ? NE : EQ);
  8888.       op1 = const0_rtx, const_op = 0;
  8889.     }
  8890.  
  8891.       /* Similarly, if we are comparing a value known to be either -1 or
  8892.      0 with -1, change it to the opposite comparison against zero.  */
  8893.  
  8894.       if (const_op == -1
  8895.       && (code == EQ || code == NE || code == GT || code == LE
  8896.           || code == GEU || code == LTU)
  8897.       && num_sign_bit_copies (op0, mode) == mode_width)
  8898.     {
  8899.       code = (code == EQ || code == LE || code == GEU ? NE : EQ);
  8900.       op1 = const0_rtx, const_op = 0;
  8901.     }
  8902.  
  8903.       /* Do some canonicalizations based on the comparison code.  We prefer
  8904.      comparisons against zero and then prefer equality comparisons.  
  8905.      If we can reduce the size of a constant, we will do that too.  */
  8906.  
  8907.       switch (code)
  8908.     {
  8909.     case LT:
  8910.       /* < C is equivalent to <= (C - 1) */
  8911.       if (const_op > 0)
  8912.         {
  8913.           const_op -= 1;
  8914.           op1 = GEN_INT (const_op);
  8915.           code = LE;
  8916.           /* ... fall through to LE case below.  */
  8917.         }
  8918.       else
  8919.         break;
  8920.  
  8921.     case LE:
  8922.       /* <= C is equivalent to < (C + 1); we do this for C < 0  */
  8923.       if (const_op < 0)
  8924.         {
  8925.           const_op += 1;
  8926.           op1 = GEN_INT (const_op);
  8927.           code = LT;
  8928.         }
  8929.  
  8930.       /* If we are doing a <= 0 comparison on a value known to have
  8931.          a zero sign bit, we can replace this with == 0.  */
  8932.       else if (const_op == 0
  8933.            && mode_width <= HOST_BITS_PER_WIDE_INT
  8934.            && (nonzero_bits (op0, mode)
  8935.                & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
  8936.         code = EQ;
  8937.       break;
  8938.  
  8939.     case GE:
  8940.       /* >= C is equivalent to > (C - 1). */
  8941.       if (const_op > 0)
  8942.         {
  8943.           const_op -= 1;
  8944.           op1 = GEN_INT (const_op);
  8945.           code = GT;
  8946.           /* ... fall through to GT below.  */
  8947.         }
  8948.       else
  8949.         break;
  8950.  
  8951.     case GT:
  8952.       /* > C is equivalent to >= (C + 1); we do this for C < 0*/
  8953.       if (const_op < 0)
  8954.         {
  8955.           const_op += 1;
  8956.           op1 = GEN_INT (const_op);
  8957.           code = GE;
  8958.         }
  8959.  
  8960.       /* If we are doing a > 0 comparison on a value known to have
  8961.          a zero sign bit, we can replace this with != 0.  */
  8962.       else if (const_op == 0
  8963.            && mode_width <= HOST_BITS_PER_WIDE_INT
  8964.            && (nonzero_bits (op0, mode)
  8965.                & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
  8966.         code = NE;
  8967.       break;
  8968.  
  8969.     case LTU:
  8970.       /* < C is equivalent to <= (C - 1).  */
  8971.       if (const_op > 0)
  8972.         {
  8973.           const_op -= 1;
  8974.           op1 = GEN_INT (const_op);
  8975.           code = LEU;
  8976.           /* ... fall through ... */
  8977.         }
  8978.  
  8979.       /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
  8980.       else if (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1))
  8981.         {
  8982.           const_op = 0, op1 = const0_rtx;
  8983.           code = GE;
  8984.           break;
  8985.         }
  8986.       else
  8987.         break;
  8988.  
  8989.     case LEU:
  8990.       /* unsigned <= 0 is equivalent to == 0 */
  8991.       if (const_op == 0)
  8992.         code = EQ;
  8993.  
  8994.       /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
  8995.       else if (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
  8996.         {
  8997.           const_op = 0, op1 = const0_rtx;
  8998.           code = GE;
  8999.         }
  9000.       break;
  9001.  
  9002.     case GEU:
  9003.       /* >= C is equivalent to < (C - 1).  */
  9004.       if (const_op > 1)
  9005.         {
  9006.           const_op -= 1;
  9007.           op1 = GEN_INT (const_op);
  9008.           code = GTU;
  9009.           /* ... fall through ... */
  9010.         }
  9011.  
  9012.       /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
  9013.       else if (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1))
  9014.         {
  9015.           const_op = 0, op1 = const0_rtx;
  9016.           code = LT;
  9017.           break;
  9018.         }
  9019.       else
  9020.         break;
  9021.  
  9022.     case GTU:
  9023.       /* unsigned > 0 is equivalent to != 0 */
  9024.       if (const_op == 0)
  9025.         code = NE;
  9026.  
  9027.       /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
  9028.       else if (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
  9029.         {
  9030.           const_op = 0, op1 = const0_rtx;
  9031.           code = LT;
  9032.         }
  9033.       break;
  9034.     }
  9035.  
  9036.       /* Compute some predicates to simplify code below.  */
  9037.  
  9038.       equality_comparison_p = (code == EQ || code == NE);
  9039.       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
  9040.       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
  9041.                    || code == LEU);
  9042.  
  9043.       /* If this is a sign bit comparison and we can do arithmetic in
  9044.      MODE, say that we will only be needing the sign bit of OP0.  */
  9045.       if (sign_bit_comparison_p
  9046.       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
  9047.     op0 = force_to_mode (op0, mode,
  9048.                  ((HOST_WIDE_INT) 1
  9049.                   << (GET_MODE_BITSIZE (mode) - 1)),
  9050.                  NULL_RTX, 0);
  9051.  
  9052.       /* Now try cases based on the opcode of OP0.  If none of the cases
  9053.      does a "continue", we exit this loop immediately after the
  9054.      switch.  */
  9055.  
  9056.       switch (GET_CODE (op0))
  9057.     {
  9058.     case ZERO_EXTRACT:
  9059.       /* If we are extracting a single bit from a variable position in
  9060.          a constant that has only a single bit set and are comparing it
  9061.          with zero, we can convert this into an equality comparison 
  9062.          between the position and the location of the single bit.  We can't
  9063.          do this if bit endian and we don't have an extzv since we then
  9064.          can't know what mode to use for the endianness adjustment.  */
  9065.  
  9066.       if (GET_CODE (XEXP (op0, 0)) == CONST_INT
  9067.           && XEXP (op0, 1) == const1_rtx
  9068.           && equality_comparison_p && const_op == 0
  9069.           && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0
  9070.           && (! BITS_BIG_ENDIAN
  9071. #ifdef HAVE_extzv
  9072.           || HAVE_extzv
  9073. #endif
  9074.           ))
  9075.         {
  9076. #ifdef HAVE_extzv
  9077.           if (BITS_BIG_ENDIAN)
  9078.         i = (GET_MODE_BITSIZE
  9079.              (insn_operand_mode[(int) CODE_FOR_extzv][1]) - 1 - i);
  9080. #endif
  9081.  
  9082.           op0 = XEXP (op0, 2);
  9083.           op1 = GEN_INT (i);
  9084.           const_op = i;
  9085.  
  9086.           /* Result is nonzero iff shift count is equal to I.  */
  9087.           code = reverse_condition (code);
  9088.           continue;
  9089.         }
  9090.  
  9091.       /* ... fall through ... */
  9092.  
  9093.     case SIGN_EXTRACT:
  9094.       tem = expand_compound_operation (op0);
  9095.       if (tem != op0)
  9096.         {
  9097.           op0 = tem;
  9098.           continue;
  9099.         }
  9100.       break;
  9101.  
  9102.     case NOT:
  9103.       /* If testing for equality, we can take the NOT of the constant.  */
  9104.       if (equality_comparison_p
  9105.           && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
  9106.         {
  9107.           op0 = XEXP (op0, 0);
  9108.           op1 = tem;
  9109.           continue;
  9110.         }
  9111.  
  9112.       /* If just looking at the sign bit, reverse the sense of the
  9113.          comparison.  */
  9114.       if (sign_bit_comparison_p)
  9115.         {
  9116.           op0 = XEXP (op0, 0);
  9117.           code = (code == GE ? LT : GE);
  9118.           continue;
  9119.         }
  9120.       break;
  9121.  
  9122.     case NEG:
  9123.       /* If testing for equality, we can take the NEG of the constant.  */
  9124.       if (equality_comparison_p
  9125.           && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
  9126.         {
  9127.           op0 = XEXP (op0, 0);
  9128.           op1 = tem;
  9129.           continue;
  9130.         }
  9131.  
  9132.       /* The remaining cases only apply to comparisons with zero.  */
  9133.       if (const_op != 0)
  9134.         break;
  9135.  
  9136.       /* When X is ABS or is known positive,
  9137.          (neg X) is < 0 if and only if X != 0.  */
  9138.  
  9139.       if (sign_bit_comparison_p
  9140.           && (GET_CODE (XEXP (op0, 0)) == ABS
  9141.           || (mode_width <= HOST_BITS_PER_WIDE_INT
  9142.               && (nonzero_bits (XEXP (op0, 0), mode)
  9143.               & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
  9144.         {
  9145.           op0 = XEXP (op0, 0);
  9146.           code = (code == LT ? NE : EQ);
  9147.           continue;
  9148.         }
  9149.  
  9150.       /* If we have NEG of something whose two high-order bits are the
  9151.          same, we know that "(-a) < 0" is equivalent to "a > 0". */
  9152.       if (num_sign_bit_copies (op0, mode) >= 2)
  9153.         {
  9154.           op0 = XEXP (op0, 0);
  9155.           code = swap_condition (code);
  9156.           continue;
  9157.         }
  9158.       break;
  9159.  
  9160.     case ROTATE:
  9161.       /* If we are testing equality and our count is a constant, we
  9162.          can perform the inverse operation on our RHS.  */
  9163.       if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9164.           && (tem = simplify_binary_operation (ROTATERT, mode,
  9165.                            op1, XEXP (op0, 1))) != 0)
  9166.         {
  9167.           op0 = XEXP (op0, 0);
  9168.           op1 = tem;
  9169.           continue;
  9170.         }
  9171.  
  9172.       /* If we are doing a < 0 or >= 0 comparison, it means we are testing
  9173.          a particular bit.  Convert it to an AND of a constant of that
  9174.          bit.  This will be converted into a ZERO_EXTRACT.  */
  9175.       if (const_op == 0 && sign_bit_comparison_p
  9176.           && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9177.           && mode_width <= HOST_BITS_PER_WIDE_INT)
  9178.         {
  9179.           op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
  9180.                         ((HOST_WIDE_INT) 1
  9181.                          << (mode_width - 1
  9182.                          - INTVAL (XEXP (op0, 1)))));
  9183.           code = (code == LT ? NE : EQ);
  9184.           continue;
  9185.         }
  9186.  
  9187.       /* ... fall through ... */
  9188.  
  9189.     case ABS:
  9190.       /* ABS is ignorable inside an equality comparison with zero.  */
  9191.       if (const_op == 0 && equality_comparison_p)
  9192.         {
  9193.           op0 = XEXP (op0, 0);
  9194.           continue;
  9195.         }
  9196.       break;
  9197.       
  9198.  
  9199.     case SIGN_EXTEND:
  9200.       /* Can simplify (compare (zero/sign_extend FOO) CONST)
  9201.          to (compare FOO CONST) if CONST fits in FOO's mode and we 
  9202.          are either testing inequality or have an unsigned comparison
  9203.          with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
  9204.       if (! unsigned_comparison_p
  9205.           && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
  9206.           <= HOST_BITS_PER_WIDE_INT)
  9207.           && ((unsigned HOST_WIDE_INT) const_op
  9208.           < (((HOST_WIDE_INT) 1
  9209.               << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
  9210.         {
  9211.           op0 = XEXP (op0, 0);
  9212.           continue;
  9213.         }
  9214.       break;
  9215.  
  9216.     case SUBREG:
  9217.       /* Check for the case where we are comparing A - C1 with C2,
  9218.          both constants are smaller than 1/2 the maxium positive
  9219.          value in MODE, and the comparison is equality or unsigned.
  9220.          In that case, if A is either zero-extended to MODE or has
  9221.          sufficient sign bits so that the high-order bit in MODE
  9222.          is a copy of the sign in the inner mode, we can prove that it is
  9223.          safe to do the operation in the wider mode.  This simplifies
  9224.          many range checks.  */
  9225.  
  9226.       if (mode_width <= HOST_BITS_PER_WIDE_INT
  9227.           && subreg_lowpart_p (op0)
  9228.           && GET_CODE (SUBREG_REG (op0)) == PLUS
  9229.           && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
  9230.           && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
  9231.           && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
  9232.           < GET_MODE_MASK (mode) / 2)
  9233.           && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
  9234.           && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
  9235.                       GET_MODE (SUBREG_REG (op0)))
  9236.             & ~ GET_MODE_MASK (mode))
  9237.           || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
  9238.                        GET_MODE (SUBREG_REG (op0)))
  9239.               > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
  9240.              - GET_MODE_BITSIZE (mode)))))
  9241.         {
  9242.           op0 = SUBREG_REG (op0);
  9243.           continue;
  9244.         }
  9245.  
  9246.       /* If the inner mode is narrower and we are extracting the low part,
  9247.          we can treat the SUBREG as if it were a ZERO_EXTEND.  */
  9248.       if (subreg_lowpart_p (op0)
  9249.           && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
  9250.         /* Fall through */ ;
  9251.       else
  9252.         break;
  9253.  
  9254.       /* ... fall through ... */
  9255.  
  9256.     case ZERO_EXTEND:
  9257.       if ((unsigned_comparison_p || equality_comparison_p)
  9258.           && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
  9259.           <= HOST_BITS_PER_WIDE_INT)
  9260.           && ((unsigned HOST_WIDE_INT) const_op
  9261.           < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
  9262.         {
  9263.           op0 = XEXP (op0, 0);
  9264.           continue;
  9265.         }
  9266.       break;
  9267.  
  9268.     case PLUS:
  9269.       /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
  9270.          this for equality comparisons due to pathological cases involving
  9271.          overflows.  */
  9272.       if (equality_comparison_p
  9273.           && 0 != (tem = simplify_binary_operation (MINUS, mode,
  9274.                             op1, XEXP (op0, 1))))
  9275.         {
  9276.           op0 = XEXP (op0, 0);
  9277.           op1 = tem;
  9278.           continue;
  9279.         }
  9280.  
  9281.       /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
  9282.       if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
  9283.           && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
  9284.         {
  9285.           op0 = XEXP (XEXP (op0, 0), 0);
  9286.           code = (code == LT ? EQ : NE);
  9287.           continue;
  9288.         }
  9289.       break;
  9290.  
  9291.     case MINUS:
  9292.       /* (eq (minus A B) C) -> (eq A (plus B C)) or
  9293.          (eq B (minus A C)), whichever simplifies.  We can only do
  9294.          this for equality comparisons due to pathological cases involving
  9295.          overflows.  */
  9296.       if (equality_comparison_p
  9297.           && 0 != (tem = simplify_binary_operation (PLUS, mode,
  9298.                             XEXP (op0, 1), op1)))
  9299.         {
  9300.           op0 = XEXP (op0, 0);
  9301.           op1 = tem;
  9302.           continue;
  9303.         }
  9304.  
  9305.       if (equality_comparison_p
  9306.           && 0 != (tem = simplify_binary_operation (MINUS, mode,
  9307.                             XEXP (op0, 0), op1)))
  9308.         {
  9309.           op0 = XEXP (op0, 1);
  9310.           op1 = tem;
  9311.           continue;
  9312.         }
  9313.  
  9314.       /* The sign bit of (minus (ashiftrt X C) X), where C is the number
  9315.          of bits in X minus 1, is one iff X > 0.  */
  9316.       if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
  9317.           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
  9318.           && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
  9319.           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
  9320.         {
  9321.           op0 = XEXP (op0, 1);
  9322.           code = (code == GE ? LE : GT);
  9323.           continue;
  9324.         }
  9325.       break;
  9326.  
  9327.     case XOR:
  9328.       /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
  9329.          if C is zero or B is a constant.  */
  9330.       if (equality_comparison_p
  9331.           && 0 != (tem = simplify_binary_operation (XOR, mode,
  9332.                             XEXP (op0, 1), op1)))
  9333.         {
  9334.           op0 = XEXP (op0, 0);
  9335.           op1 = tem;
  9336.           continue;
  9337.         }
  9338.       break;
  9339.  
  9340.     case EQ:  case NE:
  9341.     case LT:  case LTU:  case LE:  case LEU:
  9342.     case GT:  case GTU:  case GE:  case GEU:
  9343.       /* We can't do anything if OP0 is a condition code value, rather
  9344.          than an actual data value.  */
  9345.       if (const_op != 0
  9346. #ifdef HAVE_cc0
  9347.           || XEXP (op0, 0) == cc0_rtx
  9348. #endif
  9349.           || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
  9350.         break;
  9351.  
  9352.       /* Get the two operands being compared.  */
  9353.       if (GET_CODE (XEXP (op0, 0)) == COMPARE)
  9354.         tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
  9355.       else
  9356.         tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
  9357.  
  9358.       /* Check for the cases where we simply want the result of the
  9359.          earlier test or the opposite of that result.  */
  9360.       if (code == NE
  9361.           || (code == EQ && reversible_comparison_p (op0))
  9362.           || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
  9363.           && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
  9364.           && (STORE_FLAG_VALUE
  9365.               & (((HOST_WIDE_INT) 1
  9366.               << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
  9367.           && (code == LT
  9368.               || (code == GE && reversible_comparison_p (op0)))))
  9369.         {
  9370.           code = (code == LT || code == NE
  9371.               ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
  9372.           op0 = tem, op1 = tem1;
  9373.           continue;
  9374.         }
  9375.       break;
  9376.  
  9377.     case IOR:
  9378.       /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
  9379.          iff X <= 0.  */
  9380.       if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
  9381.           && XEXP (XEXP (op0, 0), 1) == constm1_rtx
  9382.           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
  9383.         {
  9384.           op0 = XEXP (op0, 1);
  9385.           code = (code == GE ? GT : LE);
  9386.           continue;
  9387.         }
  9388.       break;
  9389.  
  9390.     case AND:
  9391.       /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
  9392.          will be converted to a ZERO_EXTRACT later.  */
  9393.       if (const_op == 0 && equality_comparison_p
  9394.           && GET_CODE (XEXP (op0, 0)) == ASHIFT
  9395.           && XEXP (XEXP (op0, 0), 0) == const1_rtx)
  9396.         {
  9397.           op0 = simplify_and_const_int
  9398.         (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
  9399.                          XEXP (op0, 1),
  9400.                          XEXP (XEXP (op0, 0), 1)),
  9401.          (HOST_WIDE_INT) 1);
  9402.           continue;
  9403.         }
  9404.  
  9405.       /* If we are comparing (and (lshiftrt X C1) C2) for equality with
  9406.          zero and X is a comparison and C1 and C2 describe only bits set
  9407.          in STORE_FLAG_VALUE, we can compare with X.  */
  9408.       if (const_op == 0 && equality_comparison_p
  9409.           && mode_width <= HOST_BITS_PER_WIDE_INT
  9410.           && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9411.           && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
  9412.           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
  9413.           && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
  9414.           && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
  9415.         {
  9416.           mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
  9417.               << INTVAL (XEXP (XEXP (op0, 0), 1)));
  9418.           if ((~ STORE_FLAG_VALUE & mask) == 0
  9419.           && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
  9420.               || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
  9421.               && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
  9422.         {
  9423.           op0 = XEXP (XEXP (op0, 0), 0);
  9424.           continue;
  9425.         }
  9426.         }
  9427.  
  9428.       /* If we are doing an equality comparison of an AND of a bit equal
  9429.          to the sign bit, replace this with a LT or GE comparison of
  9430.          the underlying value.  */
  9431.       if (equality_comparison_p
  9432.           && const_op == 0
  9433.           && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9434.           && mode_width <= HOST_BITS_PER_WIDE_INT
  9435.           && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
  9436.           == (HOST_WIDE_INT) 1 << (mode_width - 1)))
  9437.         {
  9438.           op0 = XEXP (op0, 0);
  9439.           code = (code == EQ ? GE : LT);
  9440.           continue;
  9441.         }
  9442.  
  9443.       /* If this AND operation is really a ZERO_EXTEND from a narrower
  9444.          mode, the constant fits within that mode, and this is either an
  9445.          equality or unsigned comparison, try to do this comparison in
  9446.          the narrower mode.  */
  9447.       if ((equality_comparison_p || unsigned_comparison_p)
  9448.           && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9449.           && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
  9450.                    & GET_MODE_MASK (mode))
  9451.                   + 1)) >= 0
  9452.           && const_op >> i == 0
  9453.           && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
  9454.         {
  9455.           op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
  9456.           continue;
  9457.         }
  9458.       break;
  9459.  
  9460.     case ASHIFT:
  9461.       /* If we have (compare (ashift FOO N) (const_int C)) and
  9462.          the high order N bits of FOO (N+1 if an inequality comparison)
  9463.          are known to be zero, we can do this by comparing FOO with C
  9464.          shifted right N bits so long as the low-order N bits of C are
  9465.          zero.  */
  9466.       if (GET_CODE (XEXP (op0, 1)) == CONST_INT
  9467.           && INTVAL (XEXP (op0, 1)) >= 0
  9468.           && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
  9469.           < HOST_BITS_PER_WIDE_INT)
  9470.           && ((const_op
  9471.            & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
  9472.           && mode_width <= HOST_BITS_PER_WIDE_INT
  9473.           && (nonzero_bits (XEXP (op0, 0), mode)
  9474.           & ~ (mask >> (INTVAL (XEXP (op0, 1))
  9475.                 + ! equality_comparison_p))) == 0)
  9476.         {
  9477.           const_op >>= INTVAL (XEXP (op0, 1));
  9478.           op1 = GEN_INT (const_op);
  9479.           op0 = XEXP (op0, 0);
  9480.           continue;
  9481.         }
  9482.  
  9483.       /* If we are doing a sign bit comparison, it means we are testing
  9484.          a particular bit.  Convert it to the appropriate AND.  */
  9485.       if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9486.           && mode_width <= HOST_BITS_PER_WIDE_INT)
  9487.         {
  9488.           op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
  9489.                         ((HOST_WIDE_INT) 1
  9490.                          << (mode_width - 1
  9491.                          - INTVAL (XEXP (op0, 1)))));
  9492.           code = (code == LT ? NE : EQ);
  9493.           continue;
  9494.         }
  9495.  
  9496.       /* If this an equality comparison with zero and we are shifting
  9497.          the low bit to the sign bit, we can convert this to an AND of the
  9498.          low-order bit.  */
  9499.       if (const_op == 0 && equality_comparison_p
  9500.           && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9501.           && INTVAL (XEXP (op0, 1)) == mode_width - 1)
  9502.         {
  9503.           op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
  9504.                         (HOST_WIDE_INT) 1);
  9505.           continue;
  9506.         }
  9507.       break;
  9508.  
  9509.     case ASHIFTRT:
  9510.       /* If this is an equality comparison with zero, we can do this
  9511.          as a logical shift, which might be much simpler.  */
  9512.       if (equality_comparison_p && const_op == 0
  9513.           && GET_CODE (XEXP (op0, 1)) == CONST_INT)
  9514.         {
  9515.           op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
  9516.                       XEXP (op0, 0),
  9517.                       INTVAL (XEXP (op0, 1)));
  9518.           continue;
  9519.         }
  9520.  
  9521.       /* If OP0 is a sign extension and CODE is not an unsigned comparison,
  9522.          do the comparison in a narrower mode.  */
  9523.       if (! unsigned_comparison_p
  9524.           && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9525.           && GET_CODE (XEXP (op0, 0)) == ASHIFT
  9526.           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
  9527.           && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
  9528.                      MODE_INT, 1)) != BLKmode
  9529.           && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
  9530.           || ((unsigned HOST_WIDE_INT) - const_op
  9531.               <= GET_MODE_MASK (tmode))))
  9532.         {
  9533.           op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
  9534.           continue;
  9535.         }
  9536.  
  9537.       /* ... fall through ... */
  9538.     case LSHIFTRT:
  9539.       /* If we have (compare (xshiftrt FOO N) (const_int C)) and
  9540.          the low order N bits of FOO are known to be zero, we can do this
  9541.          by comparing FOO with C shifted left N bits so long as no
  9542.          overflow occurs.  */
  9543.       if (GET_CODE (XEXP (op0, 1)) == CONST_INT
  9544.           && INTVAL (XEXP (op0, 1)) >= 0
  9545.           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
  9546.           && mode_width <= HOST_BITS_PER_WIDE_INT
  9547.           && (nonzero_bits (XEXP (op0, 0), mode)
  9548.           & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
  9549.           && (const_op == 0
  9550.           || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
  9551.               < mode_width)))
  9552.         {
  9553.           const_op <<= INTVAL (XEXP (op0, 1));
  9554.           op1 = GEN_INT (const_op);
  9555.           op0 = XEXP (op0, 0);
  9556.           continue;
  9557.         }
  9558.  
  9559.       /* If we are using this shift to extract just the sign bit, we
  9560.          can replace this with an LT or GE comparison.  */
  9561.       if (const_op == 0
  9562.           && (equality_comparison_p || sign_bit_comparison_p)
  9563.           && GET_CODE (XEXP (op0, 1)) == CONST_INT
  9564.           && INTVAL (XEXP (op0, 1)) == mode_width - 1)
  9565.         {
  9566.           op0 = XEXP (op0, 0);
  9567.           code = (code == NE || code == GT ? LT : GE);
  9568.           continue;
  9569.         }
  9570.       break;
  9571.     }
  9572.  
  9573.       break;
  9574.     }
  9575.  
  9576.   /* Now make any compound operations involved in this comparison.  Then,
  9577.      check for an outmost SUBREG on OP0 that isn't doing anything or is
  9578.      paradoxical.  The latter case can only occur when it is known that the
  9579.      "extra" bits will be zero.  Therefore, it is safe to remove the SUBREG.
  9580.      We can never remove a SUBREG for a non-equality comparison because the
  9581.      sign bit is in a different place in the underlying object.  */
  9582.  
  9583.   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
  9584.   op1 = make_compound_operation (op1, SET);
  9585.  
  9586.   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
  9587.       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
  9588.       && (code == NE || code == EQ)
  9589.       && ((GET_MODE_SIZE (GET_MODE (op0))
  9590.        > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
  9591.     {
  9592.       op0 = SUBREG_REG (op0);
  9593.       op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
  9594.     }
  9595.  
  9596.   else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
  9597.        && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
  9598.        && (code == NE || code == EQ)
  9599.        && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
  9600.            <= HOST_BITS_PER_WIDE_INT)
  9601.        && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
  9602.            & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
  9603.        && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
  9604.                           op1),
  9605.            (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
  9606.         & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
  9607.     op0 = SUBREG_REG (op0), op1 = tem;
  9608.  
  9609.   /* We now do the opposite procedure: Some machines don't have compare
  9610.      insns in all modes.  If OP0's mode is an integer mode smaller than a
  9611.      word and we can't do a compare in that mode, see if there is a larger
  9612.      mode for which we can do the compare.  There are a number of cases in
  9613.      which we can use the wider mode.  */
  9614.  
  9615.   mode = GET_MODE (op0);
  9616.   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
  9617.       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
  9618.       && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
  9619.     for (tmode = GET_MODE_WIDER_MODE (mode);
  9620.      (tmode != VOIDmode
  9621.       && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
  9622.      tmode = GET_MODE_WIDER_MODE (tmode))
  9623.       if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
  9624.     {
  9625.       /* If the only nonzero bits in OP0 and OP1 are those in the
  9626.          narrower mode and this is an equality or unsigned comparison,
  9627.          we can use the wider mode.  Similarly for sign-extended
  9628.          values, in which case it is true for all comparisons.  */
  9629.       if (((code == EQ || code == NE
  9630.         || code == GEU || code == GTU || code == LEU || code == LTU)
  9631.            && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
  9632.            && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
  9633.           || ((num_sign_bit_copies (op0, tmode)
  9634.            > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
  9635.           && (num_sign_bit_copies (op1, tmode)
  9636.               > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
  9637.         {
  9638.           op0 = gen_lowpart_for_combine (tmode, op0);
  9639.           op1 = gen_lowpart_for_combine (tmode, op1);
  9640.           break;
  9641.         }
  9642.  
  9643.       /* If this is a test for negative, we can make an explicit
  9644.          test of the sign bit.  */
  9645.  
  9646.       if (op1 == const0_rtx && (code == LT || code == GE)
  9647.           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
  9648.         {
  9649.           op0 = gen_binary (AND, tmode,
  9650.                 gen_lowpart_for_combine (tmode, op0),
  9651.                 GEN_INT ((HOST_WIDE_INT) 1
  9652.                      << (GET_MODE_BITSIZE (mode) - 1)));
  9653.           code = (code == LT) ? NE : EQ;
  9654.           break;
  9655.         }
  9656.     }
  9657.  
  9658. #ifdef CANONICALIZE_COMPARISON
  9659.   /* If this machine only supports a subset of valid comparisons, see if we
  9660.      can convert an unsupported one into a supported one.  */
  9661.   CANONICALIZE_COMPARISON (code, op0, op1);
  9662. #endif
  9663.  
  9664.   *pop0 = op0;
  9665.   *pop1 = op1;
  9666.  
  9667.   return code;
  9668. }
  9669.  
  9670. /* Return 1 if we know that X, a comparison operation, is not operating
  9671.    on a floating-point value or is EQ or NE, meaning that we can safely
  9672.    reverse it.  */
  9673.  
  9674. static int
  9675. reversible_comparison_p (x)
  9676.      rtx x;
  9677. {
  9678.   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  9679.       || flag_fast_math
  9680.       || GET_CODE (x) == NE || GET_CODE (x) == EQ)
  9681.     return 1;
  9682.  
  9683.   switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
  9684.     {
  9685.     case MODE_INT:
  9686.     case MODE_PARTIAL_INT:
  9687.     case MODE_COMPLEX_INT:
  9688.       return 1;
  9689.  
  9690.     case MODE_CC:
  9691.       /* If the mode of the condition codes tells us that this is safe,
  9692.      we need look no further.  */
  9693.       if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
  9694.     return 1;
  9695.  
  9696.       /* Otherwise try and find where the condition codes were last set and
  9697.      use that.  */
  9698.       x = get_last_value (XEXP (x, 0));
  9699.       return (x && GET_CODE (x) == COMPARE
  9700.           && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
  9701.     }
  9702.  
  9703.   return 0;
  9704. }
  9705.  
  9706. /* Utility function for following routine.  Called when X is part of a value
  9707.    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
  9708.    for each register mentioned.  Similar to mention_regs in cse.c  */
  9709.  
  9710. static void
  9711. update_table_tick (x)
  9712.      rtx x;
  9713. {
  9714.   register enum rtx_code code = GET_CODE (x);
  9715.   register char *fmt = GET_RTX_FORMAT (code);
  9716.   register int i;
  9717.  
  9718.   if (code == REG)
  9719.     {
  9720.       int regno = REGNO (x);
  9721.       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
  9722.                   ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
  9723.  
  9724.       for (i = regno; i < endregno; i++)
  9725.     reg_last_set_table_tick[i] = label_tick;
  9726.  
  9727.       return;
  9728.     }
  9729.   
  9730.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  9731.     /* Note that we can't have an "E" in values stored; see
  9732.        get_last_value_validate.  */
  9733.     if (fmt[i] == 'e')
  9734.       update_table_tick (XEXP (x, i));
  9735. }
  9736.  
  9737. /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
  9738.    are saying that the register is clobbered and we no longer know its
  9739.    value.  If INSN is zero, don't update reg_last_set; this is only permitted
  9740.    with VALUE also zero and is used to invalidate the register.  */
  9741.  
  9742. static void
  9743. record_value_for_reg (reg, insn, value)
  9744.      rtx reg;
  9745.      rtx insn;
  9746.      rtx value;
  9747. {
  9748.   int regno = REGNO (reg);
  9749.   int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
  9750.               ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
  9751.   int i;
  9752.  
  9753.   /* If VALUE contains REG and we have a previous value for REG, substitute
  9754.      the previous value.  */
  9755.   if (value && insn && reg_overlap_mentioned_p (reg, value))
  9756.     {
  9757.       rtx tem;
  9758.  
  9759.       /* Set things up so get_last_value is allowed to see anything set up to
  9760.      our insn.  */
  9761.       subst_low_cuid = INSN_CUID (insn);
  9762.       tem = get_last_value (reg);      
  9763.  
  9764.       if (tem)
  9765.     value = replace_rtx (copy_rtx (value), reg, tem);
  9766.     }
  9767.  
  9768.   /* For each register modified, show we don't know its value, that
  9769.      we don't know about its bitwise content, that its value has been
  9770.      updated, and that we don't know the location of the death of the
  9771.      register.  */
  9772.   for (i = regno; i < endregno; i ++)
  9773.     {
  9774.       if (insn)
  9775.     reg_last_set[i] = insn;
  9776.       reg_last_set_value[i] = 0;
  9777.       reg_last_set_mode[i] = 0;
  9778.       reg_last_set_nonzero_bits[i] = 0;
  9779.       reg_last_set_sign_bit_copies[i] = 0;
  9780.       reg_last_death[i] = 0;
  9781.     }
  9782.  
  9783.   /* Mark registers that are being referenced in this value.  */
  9784.   if (value)
  9785.     update_table_tick (value);
  9786.  
  9787.   /* Now update the status of each register being set.
  9788.      If someone is using this register in this block, set this register
  9789.      to invalid since we will get confused between the two lives in this
  9790.      basic block.  This makes using this register always invalid.  In cse, we
  9791.      scan the table to invalidate all entries using this register, but this
  9792.      is too much work for us.  */
  9793.  
  9794.   for (i = regno; i < endregno; i++)
  9795.     {
  9796.       reg_last_set_label[i] = label_tick;
  9797.       if (value && reg_last_set_table_tick[i] == label_tick)
  9798.     reg_last_set_invalid[i] = 1;
  9799.       else
  9800.     reg_last_set_invalid[i] = 0;
  9801.     }
  9802.  
  9803.   /* The value being assigned might refer to X (like in "x++;").  In that
  9804.      case, we must replace it with (clobber (const_int 0)) to prevent
  9805.      infinite loops.  */
  9806.   if (value && ! get_last_value_validate (&value,
  9807.                       reg_last_set_label[regno], 0))
  9808.     {
  9809.       value = copy_rtx (value);
  9810.       if (! get_last_value_validate (&value, reg_last_set_label[regno], 1))
  9811.     value = 0;
  9812.     }
  9813.  
  9814.   /* For the main register being modified, update the value, the mode, the
  9815.      nonzero bits, and the number of sign bit copies.  */
  9816.  
  9817.   reg_last_set_value[regno] = value;
  9818.  
  9819.   if (value)
  9820.     {
  9821.       subst_low_cuid = INSN_CUID (insn);
  9822.       reg_last_set_mode[regno] = GET_MODE (reg);
  9823.       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
  9824.       reg_last_set_sign_bit_copies[regno]
  9825.     = num_sign_bit_copies (value, GET_MODE (reg));
  9826.     }
  9827. }
  9828.  
  9829. /* Used for communication between the following two routines.  */
  9830. static rtx record_dead_insn;
  9831.  
  9832. /* Called via note_stores from record_dead_and_set_regs to handle one
  9833.    SET or CLOBBER in an insn.  */
  9834.  
  9835. static void
  9836. record_dead_and_set_regs_1 (dest, setter)
  9837.      rtx dest, setter;
  9838. {
  9839.   if (GET_CODE (dest) == SUBREG)
  9840.     dest = SUBREG_REG (dest);
  9841.  
  9842.   if (GET_CODE (dest) == REG)
  9843.     {
  9844.       /* If we are setting the whole register, we know its value.  Otherwise
  9845.      show that we don't know the value.  We can handle SUBREG in
  9846.      some cases.  */
  9847.       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
  9848.     record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
  9849.       else if (GET_CODE (setter) == SET
  9850.            && GET_CODE (SET_DEST (setter)) == SUBREG
  9851.            && SUBREG_REG (SET_DEST (setter)) == dest
  9852.            && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
  9853.            && subreg_lowpart_p (SET_DEST (setter)))
  9854.     record_value_for_reg (dest, record_dead_insn,
  9855.                   gen_lowpart_for_combine (GET_MODE (dest),
  9856.                                SET_SRC (setter)));
  9857.       else
  9858.     record_value_for_reg (dest, record_dead_insn, NULL_RTX);
  9859.     }
  9860.   else if (GET_CODE (dest) == MEM
  9861.        /* Ignore pushes, they clobber nothing.  */
  9862.        && ! push_operand (dest, GET_MODE (dest)))
  9863.     mem_last_set = INSN_CUID (record_dead_insn);
  9864. }
  9865.  
  9866. /* Update the records of when each REG was most recently set or killed
  9867.    for the things done by INSN.  This is the last thing done in processing
  9868.    INSN in the combiner loop.
  9869.  
  9870.    We update reg_last_set, reg_last_set_value, reg_last_set_mode,
  9871.    reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
  9872.    and also the similar information mem_last_set (which insn most recently
  9873.    modified memory) and last_call_cuid (which insn was the most recent
  9874.    subroutine call).  */
  9875.  
  9876. static void
  9877. record_dead_and_set_regs (insn)
  9878.      rtx insn;
  9879. {
  9880.   register rtx link;
  9881.   int i;
  9882.  
  9883.   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  9884.     {
  9885.       if (REG_NOTE_KIND (link) == REG_DEAD
  9886.       && GET_CODE (XEXP (link, 0)) == REG)
  9887.     {
  9888.       int regno = REGNO (XEXP (link, 0));
  9889.       int endregno
  9890.         = regno + (regno < FIRST_PSEUDO_REGISTER
  9891.                ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
  9892.                : 1);
  9893.  
  9894.       for (i = regno; i < endregno; i++)
  9895.         reg_last_death[i] = insn;
  9896.     }
  9897.       else if (REG_NOTE_KIND (link) == REG_INC)
  9898.     record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
  9899.     }
  9900.  
  9901.   if (GET_CODE (insn) == CALL_INSN)
  9902.     {
  9903.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  9904.     if (call_used_regs[i])
  9905.       {
  9906.         reg_last_set_value[i] = 0;
  9907.         reg_last_set_mode[i] = 0;
  9908.         reg_last_set_nonzero_bits[i] = 0;
  9909.         reg_last_set_sign_bit_copies[i] = 0;
  9910.         reg_last_death[i] = 0;
  9911.       }
  9912.  
  9913.       last_call_cuid = mem_last_set = INSN_CUID (insn);
  9914.     }
  9915.  
  9916.   record_dead_insn = insn;
  9917.   note_stores (PATTERN (insn), record_dead_and_set_regs_1);
  9918. }
  9919.  
  9920. /* Utility routine for the following function.  Verify that all the registers
  9921.    mentioned in *LOC are valid when *LOC was part of a value set when
  9922.    label_tick == TICK.  Return 0 if some are not.
  9923.  
  9924.    If REPLACE is non-zero, replace the invalid reference with
  9925.    (clobber (const_int 0)) and return 1.  This replacement is useful because
  9926.    we often can get useful information about the form of a value (e.g., if
  9927.    it was produced by a shift that always produces -1 or 0) even though
  9928.    we don't know exactly what registers it was produced from.  */
  9929.  
  9930. static int
  9931. get_last_value_validate (loc, tick, replace)
  9932.      rtx *loc;
  9933.      int tick;
  9934.      int replace;
  9935. {
  9936.   rtx x = *loc;
  9937.   char *fmt = GET_RTX_FORMAT (GET_CODE (x));
  9938.   int len = GET_RTX_LENGTH (GET_CODE (x));
  9939.   int i;
  9940.  
  9941.   if (GET_CODE (x) == REG)
  9942.     {
  9943.       int regno = REGNO (x);
  9944.       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
  9945.                   ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
  9946.       int j;
  9947.  
  9948.       for (j = regno; j < endregno; j++)
  9949.     if (reg_last_set_invalid[j]
  9950.         /* If this is a pseudo-register that was only set once, it is
  9951.            always valid.  */
  9952.         || (! (regno >= FIRST_PSEUDO_REGISTER && reg_n_sets[regno] == 1)
  9953.         && reg_last_set_label[j] > tick))
  9954.       {
  9955.         if (replace)
  9956.           *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
  9957.         return replace;
  9958.       }
  9959.  
  9960.       return 1;
  9961.     }
  9962.  
  9963.   for (i = 0; i < len; i++)
  9964.     if ((fmt[i] == 'e'
  9965.      && get_last_value_validate (&XEXP (x, i), tick, replace) == 0)
  9966.     /* Don't bother with these.  They shouldn't occur anyway.  */
  9967.     || fmt[i] == 'E')
  9968.       return 0;
  9969.  
  9970.   /* If we haven't found a reason for it to be invalid, it is valid.  */
  9971.   return 1;
  9972. }
  9973.  
  9974. /* Get the last value assigned to X, if known.  Some registers
  9975.    in the value may be replaced with (clobber (const_int 0)) if their value
  9976.    is known longer known reliably.  */
  9977.  
  9978. static rtx
  9979. get_last_value (x)
  9980.      rtx x;
  9981. {
  9982.   int regno;
  9983.   rtx value;
  9984.  
  9985.   /* If this is a non-paradoxical SUBREG, get the value of its operand and
  9986.      then convert it to the desired mode.  If this is a paradoxical SUBREG,
  9987.      we cannot predict what values the "extra" bits might have. */
  9988.   if (GET_CODE (x) == SUBREG
  9989.       && subreg_lowpart_p (x)
  9990.       && (GET_MODE_SIZE (GET_MODE (x))
  9991.       <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
  9992.       && (value = get_last_value (SUBREG_REG (x))) != 0)
  9993.     return gen_lowpart_for_combine (GET_MODE (x), value);
  9994.  
  9995.   if (GET_CODE (x) != REG)
  9996.     return 0;
  9997.  
  9998.   regno = REGNO (x);
  9999.   value = reg_last_set_value[regno];
  10000.  
  10001.   /* If we don't have a value or if it isn't for this basic block, return 0. */
  10002.  
  10003.   if (value == 0
  10004.       || (reg_n_sets[regno] != 1
  10005.       && reg_last_set_label[regno] != label_tick))
  10006.     return 0;
  10007.  
  10008.   /* If the value was set in a later insn than the ones we are processing,
  10009.      we can't use it even if the register was only set once, but make a quick
  10010.      check to see if the previous insn set it to something.  This is commonly
  10011.      the case when the same pseudo is used by repeated insns.
  10012.  
  10013.      This does not work if there exists an instruction which is temporarily
  10014.      not on the insn chain.  */
  10015.  
  10016.   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
  10017.     {
  10018.       rtx insn, set;
  10019.  
  10020.       /* We can not do anything useful in this case, because there is
  10021.      an instruction which is not on the insn chain.  */
  10022.       if (subst_prev_insn)
  10023.     return 0;
  10024.  
  10025.       /* Skip over USE insns.  They are not useful here, and they may have
  10026.      been made by combine, in which case they do not have a INSN_CUID
  10027.      value.  We can't use prev_real_insn, because that would incorrectly
  10028.      take us backwards across labels.  Skip over BARRIERs also, since
  10029.      they could have been made by combine.  If we see one, we must be
  10030.      optimizing dead code, so it doesn't matter what we do.  */
  10031.       for (insn = prev_nonnote_insn (subst_insn);
  10032.        insn && ((GET_CODE (insn) == INSN
  10033.              && GET_CODE (PATTERN (insn)) == USE)
  10034.             || GET_CODE (insn) == BARRIER
  10035.             || INSN_CUID (insn) >= subst_low_cuid);
  10036.        insn = prev_nonnote_insn (insn))
  10037.     ;
  10038.  
  10039.       if (insn
  10040.       && (set = single_set (insn)) != 0
  10041.       && rtx_equal_p (SET_DEST (set), x))
  10042.     {
  10043.       value = SET_SRC (set);
  10044.  
  10045.       /* Make sure that VALUE doesn't reference X.  Replace any
  10046.          explicit references with a CLOBBER.  If there are any remaining
  10047.          references (rare), don't use the value.  */
  10048.  
  10049.       if (reg_mentioned_p (x, value))
  10050.         value = replace_rtx (copy_rtx (value), x,
  10051.                  gen_rtx (CLOBBER, GET_MODE (x), const0_rtx));
  10052.  
  10053.       if (reg_overlap_mentioned_p (x, value))
  10054.         return 0;
  10055.     }
  10056.       else
  10057.     return 0;
  10058.     }
  10059.  
  10060.   /* If the value has all its registers valid, return it.  */
  10061.   if (get_last_value_validate (&value, reg_last_set_label[regno], 0))
  10062.     return value;
  10063.  
  10064.   /* Otherwise, make a copy and replace any invalid register with
  10065.      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
  10066.  
  10067.   value = copy_rtx (value);
  10068.   if (get_last_value_validate (&value, reg_last_set_label[regno], 1))
  10069.     return value;
  10070.  
  10071.   return 0;
  10072. }
  10073.  
  10074. /* Return nonzero if expression X refers to a REG or to memory
  10075.    that is set in an instruction more recent than FROM_CUID.  */
  10076.  
  10077. static int
  10078. use_crosses_set_p (x, from_cuid)
  10079.      register rtx x;
  10080.      int from_cuid;
  10081. {
  10082.   register char *fmt;
  10083.   register int i;
  10084.   register enum rtx_code code = GET_CODE (x);
  10085.  
  10086.   if (code == REG)
  10087.     {
  10088.       register int regno = REGNO (x);
  10089.       int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
  10090.                 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
  10091.       
  10092. #ifdef PUSH_ROUNDING
  10093.       /* Don't allow uses of the stack pointer to be moved,
  10094.      because we don't know whether the move crosses a push insn.  */
  10095.       if (regno == STACK_POINTER_REGNUM)
  10096.     return 1;
  10097. #endif
  10098.       for (;regno < endreg; regno++)
  10099.     if (reg_last_set[regno]
  10100.         && INSN_CUID (reg_last_set[regno]) > from_cuid)
  10101.       return 1;
  10102.       return 0;
  10103.     }
  10104.  
  10105.   if (code == MEM && mem_last_set > from_cuid)
  10106.     return 1;
  10107.  
  10108.   fmt = GET_RTX_FORMAT (code);
  10109.  
  10110.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  10111.     {
  10112.       if (fmt[i] == 'E')
  10113.     {
  10114.       register int j;
  10115.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  10116.         if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
  10117.           return 1;
  10118.     }
  10119.       else if (fmt[i] == 'e'
  10120.            && use_crosses_set_p (XEXP (x, i), from_cuid))
  10121.     return 1;
  10122.     }
  10123.   return 0;
  10124. }
  10125.  
  10126. /* Define three variables used for communication between the following
  10127.    routines.  */
  10128.  
  10129. static int reg_dead_regno, reg_dead_endregno;
  10130. static int reg_dead_flag;
  10131.  
  10132. /* Function called via note_stores from reg_dead_at_p.
  10133.  
  10134.    If DEST is within [reg_dead_regno, reg_dead_endregno), set 
  10135.    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
  10136.  
  10137. static void
  10138. reg_dead_at_p_1 (dest, x)
  10139.      rtx dest;
  10140.      rtx x;
  10141. {
  10142.   int regno, endregno;
  10143.  
  10144.   if (GET_CODE (dest) != REG)
  10145.     return;
  10146.  
  10147.   regno = REGNO (dest);
  10148.   endregno = regno + (regno < FIRST_PSEUDO_REGISTER 
  10149.               ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
  10150.  
  10151.   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
  10152.     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
  10153. }
  10154.  
  10155. /* Return non-zero if REG is known to be dead at INSN.
  10156.  
  10157.    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
  10158.    referencing REG, it is dead.  If we hit a SET referencing REG, it is
  10159.    live.  Otherwise, see if it is live or dead at the start of the basic
  10160.    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
  10161.    must be assumed to be always live.  */
  10162.  
  10163. static int
  10164. reg_dead_at_p (reg, insn)
  10165.      rtx reg;
  10166.      rtx insn;
  10167. {
  10168.   int block, i;
  10169.  
  10170.   /* Set variables for reg_dead_at_p_1.  */
  10171.   reg_dead_regno = REGNO (reg);
  10172.   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
  10173.                     ? HARD_REGNO_NREGS (reg_dead_regno,
  10174.                                 GET_MODE (reg))
  10175.                     : 1);
  10176.  
  10177.   reg_dead_flag = 0;
  10178.  
  10179.   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  */
  10180.   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
  10181.     {
  10182.       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
  10183.     if (TEST_HARD_REG_BIT (newpat_used_regs, i))
  10184.       return 0;
  10185.     }
  10186.  
  10187.   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
  10188.      beginning of function.  */
  10189.   for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
  10190.        insn = prev_nonnote_insn (insn))
  10191.     {
  10192.       note_stores (PATTERN (insn), reg_dead_at_p_1);
  10193.       if (reg_dead_flag)
  10194.     return reg_dead_flag == 1 ? 1 : 0;
  10195.  
  10196.       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
  10197.     return 1;
  10198.     }
  10199.  
  10200.   /* Get the basic block number that we were in.  */
  10201.   if (insn == 0)
  10202.     block = 0;
  10203.   else
  10204.     {
  10205.       for (block = 0; block < n_basic_blocks; block++)
  10206.     if (insn == basic_block_head[block])
  10207.       break;
  10208.  
  10209.       if (block == n_basic_blocks)
  10210.     return 0;
  10211.     }
  10212.  
  10213.   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
  10214.     if (basic_block_live_at_start[block][i / REGSET_ELT_BITS]
  10215.     & ((REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS)))
  10216.       return 0;
  10217.  
  10218.   return 1;
  10219. }
  10220.  
  10221. /* Note hard registers in X that are used.  This code is similar to
  10222.    that in flow.c, but much simpler since we don't care about pseudos.  */
  10223.  
  10224. static void
  10225. mark_used_regs_combine (x)
  10226.      rtx x;
  10227. {
  10228.   register RTX_CODE code = GET_CODE (x);
  10229.   register int regno;
  10230.   int i;
  10231.  
  10232.   switch (code)
  10233.     {
  10234.     case LABEL_REF:
  10235.     case SYMBOL_REF:
  10236.     case CONST_INT:
  10237.     case CONST:
  10238.     case CONST_DOUBLE:
  10239.     case PC:
  10240.     case ADDR_VEC:
  10241.     case ADDR_DIFF_VEC:
  10242.     case ASM_INPUT:
  10243. #ifdef HAVE_cc0
  10244.     /* CC0 must die in the insn after it is set, so we don't need to take
  10245.        special note of it here.  */
  10246.     case CC0:
  10247. #endif
  10248.       return;
  10249.  
  10250.     case CLOBBER:
  10251.       /* If we are clobbering a MEM, mark any hard registers inside the
  10252.      address as used.  */
  10253.       if (GET_CODE (XEXP (x, 0)) == MEM)
  10254.     mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
  10255.       return;
  10256.  
  10257.     case REG:
  10258.       regno = REGNO (x);
  10259.       /* A hard reg in a wide mode may really be multiple registers.
  10260.      If so, mark all of them just like the first.  */
  10261.       if (regno < FIRST_PSEUDO_REGISTER)
  10262.     {
  10263.       /* None of this applies to the stack, frame or arg pointers */
  10264.       if (regno == STACK_POINTER_REGNUM
  10265. #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
  10266.           || regno == HARD_FRAME_POINTER_REGNUM
  10267. #endif
  10268. #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
  10269.           || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
  10270. #endif
  10271.           || regno == FRAME_POINTER_REGNUM)
  10272.         return;
  10273.  
  10274.       i = HARD_REGNO_NREGS (regno, GET_MODE (x));
  10275.       while (i-- > 0)
  10276.         SET_HARD_REG_BIT (newpat_used_regs, regno + i);
  10277.     }
  10278.       return;
  10279.  
  10280.     case SET:
  10281.       {
  10282.     /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
  10283.        the address.  */
  10284.     register rtx testreg = SET_DEST (x);
  10285.  
  10286.     while (GET_CODE (testreg) == SUBREG
  10287.            || GET_CODE (testreg) == ZERO_EXTRACT
  10288.            || GET_CODE (testreg) == SIGN_EXTRACT
  10289.            || GET_CODE (testreg) == STRICT_LOW_PART)
  10290.       testreg = XEXP (testreg, 0);
  10291.  
  10292.     if (GET_CODE (testreg) == MEM)
  10293.       mark_used_regs_combine (XEXP (testreg, 0));
  10294.  
  10295.     mark_used_regs_combine (SET_SRC (x));
  10296.     return;
  10297.       }
  10298.     }
  10299.  
  10300.   /* Recursively scan the operands of this expression.  */
  10301.  
  10302.   {
  10303.     register char *fmt = GET_RTX_FORMAT (code);
  10304.  
  10305.     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  10306.       {
  10307.         if (fmt[i] == 'e')
  10308.       mark_used_regs_combine (XEXP (x, i));
  10309.         else if (fmt[i] == 'E')
  10310.           {
  10311.             register int j;
  10312.  
  10313.             for (j = 0; j < XVECLEN (x, i); j++)
  10314.               mark_used_regs_combine (XVECEXP (x, i, j));
  10315.           }
  10316.       }
  10317.   }
  10318. }
  10319.  
  10320.  
  10321. /* Remove register number REGNO from the dead registers list of INSN.
  10322.  
  10323.    Return the note used to record the death, if there was one.  */
  10324.  
  10325. rtx
  10326. remove_death (regno, insn)
  10327.      int regno;
  10328.      rtx insn;
  10329. {
  10330.   register rtx note = find_regno_note (insn, REG_DEAD, regno);
  10331.  
  10332.   if (note)
  10333.     {
  10334.       reg_n_deaths[regno]--;
  10335.       remove_note (insn, note);
  10336.     }
  10337.  
  10338.   return note;
  10339. }
  10340.  
  10341. /* For each register (hardware or pseudo) used within expression X, if its
  10342.    death is in an instruction with cuid between FROM_CUID (inclusive) and
  10343.    TO_INSN (exclusive), put a REG_DEAD note for that register in the
  10344.    list headed by PNOTES. 
  10345.  
  10346.    This is done when X is being merged by combination into TO_INSN.  These
  10347.    notes will then be distributed as needed.  */
  10348.  
  10349. static void
  10350. move_deaths (x, from_cuid, to_insn, pnotes)
  10351.      rtx x;
  10352.      int from_cuid;
  10353.      rtx to_insn;
  10354.      rtx *pnotes;
  10355. {
  10356.   register char *fmt;
  10357.   register int len, i;
  10358.   register enum rtx_code code = GET_CODE (x);
  10359.  
  10360.   if (code == REG)
  10361.     {
  10362.       register int regno = REGNO (x);
  10363.       register rtx where_dead = reg_last_death[regno];
  10364.       register rtx before_dead, after_dead;
  10365.  
  10366.       /* WHERE_DEAD could be a USE insn made by combine, so first we
  10367.      make sure that we have insns with valid INSN_CUID values.  */
  10368.       before_dead = where_dead;
  10369.       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
  10370.     before_dead = PREV_INSN (before_dead);
  10371.       after_dead = where_dead;
  10372.       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
  10373.     after_dead = NEXT_INSN (after_dead);
  10374.  
  10375.       if (before_dead && after_dead
  10376.       && INSN_CUID (before_dead) >= from_cuid
  10377.       && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
  10378.           || (where_dead != after_dead
  10379.           && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
  10380.     {
  10381.       rtx note = remove_death (regno, where_dead);
  10382.  
  10383.       /* It is possible for the call above to return 0.  This can occur
  10384.          when reg_last_death points to I2 or I1 that we combined with.
  10385.          In that case make a new note.
  10386.  
  10387.          We must also check for the case where X is a hard register
  10388.          and NOTE is a death note for a range of hard registers
  10389.          including X.  In that case, we must put REG_DEAD notes for
  10390.          the remaining registers in place of NOTE.  */
  10391.  
  10392.       if (note != 0 && regno < FIRST_PSEUDO_REGISTER
  10393.           && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
  10394.           != GET_MODE_SIZE (GET_MODE (x))))
  10395.         {
  10396.           int deadregno = REGNO (XEXP (note, 0));
  10397.           int deadend
  10398.         = (deadregno + HARD_REGNO_NREGS (deadregno,
  10399.                          GET_MODE (XEXP (note, 0))));
  10400.           int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
  10401.           int i;
  10402.  
  10403.           for (i = deadregno; i < deadend; i++)
  10404.         if (i < regno || i >= ourend)
  10405.           REG_NOTES (where_dead)
  10406.             = gen_rtx (EXPR_LIST, REG_DEAD,
  10407.                    gen_rtx (REG, reg_raw_mode[i], i),
  10408.                    REG_NOTES (where_dead));
  10409.         }
  10410.  
  10411.       if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
  10412.         {
  10413.           XEXP (note, 1) = *pnotes;
  10414.           *pnotes = note;
  10415.         }
  10416.       else
  10417.         *pnotes = gen_rtx (EXPR_LIST, REG_DEAD, x, *pnotes);
  10418.  
  10419.       reg_n_deaths[regno]++;
  10420.     }
  10421.  
  10422.       return;
  10423.     }
  10424.  
  10425.   else if (GET_CODE (x) == SET)
  10426.     {
  10427.       rtx dest = SET_DEST (x);
  10428.  
  10429.       move_deaths (SET_SRC (x), from_cuid, to_insn, pnotes);
  10430.  
  10431.       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
  10432.      that accesses one word of a multi-word item, some
  10433.      piece of everything register in the expression is used by
  10434.      this insn, so remove any old death.  */
  10435.  
  10436.       if (GET_CODE (dest) == ZERO_EXTRACT
  10437.       || GET_CODE (dest) == STRICT_LOW_PART
  10438.       || (GET_CODE (dest) == SUBREG
  10439.           && (((GET_MODE_SIZE (GET_MODE (dest))
  10440.             + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
  10441.           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
  10442.                + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
  10443.     {
  10444.       move_deaths (dest, from_cuid, to_insn, pnotes);
  10445.       return;
  10446.     }
  10447.  
  10448.       /* If this is some other SUBREG, we know it replaces the entire
  10449.      value, so use that as the destination.  */
  10450.       if (GET_CODE (dest) == SUBREG)
  10451.     dest = SUBREG_REG (dest);
  10452.  
  10453.       /* If this is a MEM, adjust deaths of anything used in the address.
  10454.      For a REG (the only other possibility), the entire value is
  10455.      being replaced so the old value is not used in this insn.  */
  10456.  
  10457.       if (GET_CODE (dest) == MEM)
  10458.     move_deaths (XEXP (dest, 0), from_cuid, to_insn, pnotes);
  10459.       return;
  10460.     }
  10461.  
  10462.   else if (GET_CODE (x) == CLOBBER)
  10463.     return;
  10464.  
  10465.   len = GET_RTX_LENGTH (code);
  10466.   fmt = GET_RTX_FORMAT (code);
  10467.  
  10468.   for (i = 0; i < len; i++)
  10469.     {
  10470.       if (fmt[i] == 'E')
  10471.     {
  10472.       register int j;
  10473.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  10474.         move_deaths (XVECEXP (x, i, j), from_cuid, to_insn, pnotes);
  10475.     }
  10476.       else if (fmt[i] == 'e')
  10477.     move_deaths (XEXP (x, i), from_cuid, to_insn, pnotes);
  10478.     }
  10479. }
  10480.  
  10481. /* Return 1 if X is the target of a bit-field assignment in BODY, the
  10482.    pattern of an insn.  X must be a REG.  */
  10483.  
  10484. static int
  10485. reg_bitfield_target_p (x, body)
  10486.      rtx x;
  10487.      rtx body;
  10488. {
  10489.   int i;
  10490.  
  10491.   if (GET_CODE (body) == SET)
  10492.     {
  10493.       rtx dest = SET_DEST (body);
  10494.       rtx target;
  10495.       int regno, tregno, endregno, endtregno;
  10496.  
  10497.       if (GET_CODE (dest) == ZERO_EXTRACT)
  10498.     target = XEXP (dest, 0);
  10499.       else if (GET_CODE (dest) == STRICT_LOW_PART)
  10500.     target = SUBREG_REG (XEXP (dest, 0));
  10501.       else
  10502.     return 0;
  10503.  
  10504.       if (GET_CODE (target) == SUBREG)
  10505.     target = SUBREG_REG (target);
  10506.  
  10507.       if (GET_CODE (target) != REG)
  10508.     return 0;
  10509.  
  10510.       tregno = REGNO (target), regno = REGNO (x);
  10511.       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
  10512.     return target == x;
  10513.  
  10514.       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
  10515.       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
  10516.  
  10517.       return endregno > tregno && regno < endtregno;
  10518.     }
  10519.  
  10520.   else if (GET_CODE (body) == PARALLEL)
  10521.     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
  10522.       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
  10523.     return 1;
  10524.  
  10525.   return 0;
  10526. }      
  10527.  
  10528. /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
  10529.    as appropriate.  I3 and I2 are the insns resulting from the combination
  10530.    insns including FROM (I2 may be zero).
  10531.  
  10532.    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
  10533.    not need REG_DEAD notes because they are being substituted for.  This
  10534.    saves searching in the most common cases.
  10535.  
  10536.    Each note in the list is either ignored or placed on some insns, depending
  10537.    on the type of note.  */
  10538.  
  10539. static void
  10540. distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
  10541.      rtx notes;
  10542.      rtx from_insn;
  10543.      rtx i3, i2;
  10544.      rtx elim_i2, elim_i1;
  10545. {
  10546.   rtx note, next_note;
  10547.   rtx tem;
  10548.  
  10549.   for (note = notes; note; note = next_note)
  10550.     {
  10551.       rtx place = 0, place2 = 0;
  10552.  
  10553.       /* If this NOTE references a pseudo register, ensure it references
  10554.      the latest copy of that register.  */
  10555.       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
  10556.       && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
  10557.     XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
  10558.  
  10559.       next_note = XEXP (note, 1);
  10560.       switch (REG_NOTE_KIND (note))
  10561.     {
  10562.     case REG_UNUSED:
  10563.       /* Any clobbers for i3 may still exist, and so we must process
  10564.          REG_UNUSED notes from that insn.
  10565.  
  10566.          Any clobbers from i2 or i1 can only exist if they were added by
  10567.          recog_for_combine.  In that case, recog_for_combine created the
  10568.          necessary REG_UNUSED notes.  Trying to keep any original
  10569.          REG_UNUSED notes from these insns can cause incorrect output
  10570.          if it is for the same register as the original i3 dest.
  10571.          In that case, we will notice that the register is set in i3,
  10572.          and then add a REG_UNUSED note for the destination of i3, which
  10573.          is wrong.  However, it is possible to have REG_UNUSED notes from
  10574.          i2 or i1 for register which were both used and clobbered, so
  10575.          we keep notes from i2 or i1 if they will turn into REG_DEAD
  10576.          notes.  */
  10577.  
  10578.       /* If this register is set or clobbered in I3, put the note there
  10579.          unless there is one already.  */
  10580.       if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
  10581.         {
  10582.           if (from_insn != i3)
  10583.         break;
  10584.  
  10585.           if (! (GET_CODE (XEXP (note, 0)) == REG
  10586.              ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
  10587.              : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
  10588.         place = i3;
  10589.         }
  10590.       /* Otherwise, if this register is used by I3, then this register
  10591.          now dies here, so we must put a REG_DEAD note here unless there
  10592.          is one already.  */
  10593.       else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
  10594.            && ! (GET_CODE (XEXP (note, 0)) == REG
  10595.              ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
  10596.              : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
  10597.         {
  10598.           PUT_REG_NOTE_KIND (note, REG_DEAD);
  10599.           place = i3;
  10600.         }
  10601.       break;
  10602.  
  10603.     case REG_EQUAL:
  10604.     case REG_EQUIV:
  10605.     case REG_NONNEG:
  10606.       /* These notes say something about results of an insn.  We can
  10607.          only support them if they used to be on I3 in which case they
  10608.          remain on I3.  Otherwise they are ignored.
  10609.  
  10610.          If the note refers to an expression that is not a constant, we
  10611.          must also ignore the note since we cannot tell whether the
  10612.          equivalence is still true.  It might be possible to do
  10613.          slightly better than this (we only have a problem if I2DEST
  10614.          or I1DEST is present in the expression), but it doesn't
  10615.          seem worth the trouble.  */
  10616.  
  10617.       if (from_insn == i3
  10618.           && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
  10619.         place = i3;
  10620.       break;
  10621.  
  10622.     case REG_INC:
  10623.     case REG_NO_CONFLICT:
  10624.     case REG_LABEL:
  10625.       /* These notes say something about how a register is used.  They must
  10626.          be present on any use of the register in I2 or I3.  */
  10627.       if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
  10628.         place = i3;
  10629.  
  10630.       if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
  10631.         {
  10632.           if (place)
  10633.         place2 = i2;
  10634.           else
  10635.         place = i2;
  10636.         }
  10637.       break;
  10638.  
  10639.     case REG_WAS_0:
  10640.       /* It is too much trouble to try to see if this note is still
  10641.          correct in all situations.  It is better to simply delete it.  */
  10642.       break;
  10643.  
  10644.     case REG_RETVAL:
  10645.       /* If the insn previously containing this note still exists,
  10646.          put it back where it was.  Otherwise move it to the previous
  10647.          insn.  Adjust the corresponding REG_LIBCALL note.  */
  10648.       if (GET_CODE (from_insn) != NOTE)
  10649.         place = from_insn;
  10650.       else
  10651.         {
  10652.           tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
  10653.           place = prev_real_insn (from_insn);
  10654.           if (tem && place)
  10655.         XEXP (tem, 0) = place;
  10656.         }
  10657.       break;
  10658.  
  10659.     case REG_LIBCALL:
  10660.       /* This is handled similarly to REG_RETVAL.  */
  10661.       if (GET_CODE (from_insn) != NOTE)
  10662.         place = from_insn;
  10663.       else
  10664.         {
  10665.           tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
  10666.           place = next_real_insn (from_insn);
  10667.           if (tem && place)
  10668.         XEXP (tem, 0) = place;
  10669.         }
  10670.       break;
  10671.  
  10672.     case REG_DEAD:
  10673.       /* If the register is used as an input in I3, it dies there.
  10674.          Similarly for I2, if it is non-zero and adjacent to I3.
  10675.  
  10676.          If the register is not used as an input in either I3 or I2
  10677.          and it is not one of the registers we were supposed to eliminate,
  10678.          there are two possibilities.  We might have a non-adjacent I2
  10679.          or we might have somehow eliminated an additional register
  10680.          from a computation.  For example, we might have had A & B where
  10681.          we discover that B will always be zero.  In this case we will
  10682.          eliminate the reference to A.
  10683.  
  10684.          In both cases, we must search to see if we can find a previous
  10685.          use of A and put the death note there.  */
  10686.  
  10687.       if (from_insn
  10688.           && GET_CODE (from_insn) == CALL_INSN
  10689.               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
  10690.         place = from_insn;
  10691.       else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
  10692.         place = i3;
  10693.       else if (i2 != 0 && next_nonnote_insn (i2) == i3
  10694.            && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
  10695.         place = i2;
  10696.  
  10697.       if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
  10698.         break;
  10699.  
  10700.       /* If the register is used in both I2 and I3 and it dies in I3, 
  10701.          we might have added another reference to it.  If reg_n_refs
  10702.          was 2, bump it to 3.  This has to be correct since the 
  10703.          register must have been set somewhere.  The reason this is
  10704.          done is because local-alloc.c treats 2 references as a 
  10705.          special case.  */
  10706.  
  10707.       if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
  10708.           && reg_n_refs[REGNO (XEXP (note, 0))]== 2
  10709.           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
  10710.         reg_n_refs[REGNO (XEXP (note, 0))] = 3;
  10711.  
  10712.       if (place == 0)
  10713.         {
  10714.           for (tem = prev_nonnote_insn (i3);
  10715.            place == 0 && tem
  10716.            && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
  10717.            tem = prev_nonnote_insn (tem))
  10718.         {
  10719.           /* If the register is being set at TEM, see if that is all
  10720.              TEM is doing.  If so, delete TEM.  Otherwise, make this
  10721.              into a REG_UNUSED note instead.  */
  10722.           if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
  10723.             {
  10724.               rtx set = single_set (tem);
  10725.  
  10726.               /* Verify that it was the set, and not a clobber that
  10727.              modified the register.  */
  10728.  
  10729.               if (set != 0 && ! side_effects_p (SET_SRC (set))
  10730.               && rtx_equal_p (XEXP (note, 0), SET_DEST (set)))
  10731.             {
  10732.               /* Move the notes and links of TEM elsewhere.
  10733.                  This might delete other dead insns recursively. 
  10734.                  First set the pattern to something that won't use
  10735.                  any register.  */
  10736.  
  10737.               PATTERN (tem) = pc_rtx;
  10738.  
  10739.               distribute_notes (REG_NOTES (tem), tem, tem,
  10740.                         NULL_RTX, NULL_RTX, NULL_RTX);
  10741.               distribute_links (LOG_LINKS (tem));
  10742.  
  10743.               PUT_CODE (tem, NOTE);
  10744.               NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
  10745.               NOTE_SOURCE_FILE (tem) = 0;
  10746.             }
  10747.               else
  10748.             {
  10749.               PUT_REG_NOTE_KIND (note, REG_UNUSED);
  10750.               
  10751.               /*  If there isn't already a REG_UNUSED note, put one
  10752.                   here.  */
  10753.               if (! find_regno_note (tem, REG_UNUSED,
  10754.                          REGNO (XEXP (note, 0))))
  10755.                 place = tem;
  10756.               break;
  10757.               }
  10758.           }
  10759.         else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
  10760.              || (GET_CODE (tem) == CALL_INSN
  10761.                  && find_reg_fusage (tem, USE, XEXP (note, 0))))
  10762.           {
  10763.             place = tem;
  10764.             break;
  10765.           }
  10766.         }
  10767.           
  10768.           /* If we haven't found an insn for the death note and it
  10769.          is still a REG_DEAD note, but we have hit a CODE_LABEL,
  10770.          insert a USE insn for the register at that label and
  10771.          put the death node there.  This prevents problems with
  10772.          call-state tracking in caller-save.c.  */
  10773.           if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
  10774.         {
  10775.           place
  10776.             = emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (note, 0)),
  10777.                        tem);
  10778.  
  10779.           /* If this insn was emitted between blocks, then update
  10780.              basic_block_head of the current block to include it.  */
  10781.           if (basic_block_end[this_basic_block - 1] == tem)
  10782.             basic_block_head[this_basic_block] = place;
  10783.         }
  10784.         }
  10785.  
  10786.       /* If the register is set or already dead at PLACE, we needn't do
  10787.          anything with this note if it is still a REG_DEAD note.  
  10788.  
  10789.          Note that we cannot use just `dead_or_set_p' here since we can
  10790.          convert an assignment to a register into a bit-field assignment.
  10791.          Therefore, we must also omit the note if the register is the 
  10792.          target of a bitfield assignment.  */
  10793.          
  10794.       if (place && REG_NOTE_KIND (note) == REG_DEAD)
  10795.         {
  10796.           int regno = REGNO (XEXP (note, 0));
  10797.  
  10798.           if (dead_or_set_p (place, XEXP (note, 0))
  10799.           || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
  10800.         {
  10801.           /* Unless the register previously died in PLACE, clear
  10802.              reg_last_death.  [I no longer understand why this is
  10803.              being done.] */
  10804.           if (reg_last_death[regno] != place)
  10805.             reg_last_death[regno] = 0;
  10806.           place = 0;
  10807.         }
  10808.           else
  10809.         reg_last_death[regno] = place;
  10810.  
  10811.           /* If this is a death note for a hard reg that is occupying
  10812.          multiple registers, ensure that we are still using all
  10813.          parts of the object.  If we find a piece of the object
  10814.          that is unused, we must add a USE for that piece before
  10815.          PLACE and put the appropriate REG_DEAD note on it.
  10816.  
  10817.          An alternative would be to put a REG_UNUSED for the pieces
  10818.          on the insn that set the register, but that can't be done if
  10819.          it is not in the same block.  It is simpler, though less
  10820.          efficient, to add the USE insns.  */
  10821.  
  10822.           if (place && regno < FIRST_PSEUDO_REGISTER
  10823.           && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
  10824.         {
  10825.           int endregno
  10826.             = regno + HARD_REGNO_NREGS (regno,
  10827.                         GET_MODE (XEXP (note, 0)));
  10828.           int all_used = 1;
  10829.           int i;
  10830.  
  10831.           for (i = regno; i < endregno; i++)
  10832.             if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
  10833.             && ! find_regno_fusage (place, USE, i))
  10834.               {
  10835.             rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
  10836.             rtx p;
  10837.  
  10838.             /* See if we already placed a USE note for this
  10839.                register in front of PLACE.  */
  10840.             for (p = place;
  10841.                  GET_CODE (PREV_INSN (p)) == INSN
  10842.                  && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
  10843.                  p = PREV_INSN (p))
  10844.               if (rtx_equal_p (piece,
  10845.                        XEXP (PATTERN (PREV_INSN (p)), 0)))
  10846.                 {
  10847.                   p = 0;
  10848.                   break;
  10849.                 }
  10850.  
  10851.             if (p)
  10852.               {
  10853.                 rtx use_insn
  10854.                   = emit_insn_before (gen_rtx (USE, VOIDmode,
  10855.                                piece),
  10856.                           p);
  10857.                 REG_NOTES (use_insn)
  10858.                   = gen_rtx (EXPR_LIST, REG_DEAD, piece,
  10859.                      REG_NOTES (use_insn));
  10860.               }
  10861.  
  10862.             all_used = 0;
  10863.               }
  10864.  
  10865.           /* Check for the case where the register dying partially
  10866.              overlaps the register set by this insn.  */
  10867.           if (all_used)
  10868.             for (i = regno; i < endregno; i++)
  10869.               if (dead_or_set_regno_p (place, i))
  10870.               {
  10871.                 all_used = 0;
  10872.                 break;
  10873.               }
  10874.  
  10875.           if (! all_used)
  10876.             {
  10877.               /* Put only REG_DEAD notes for pieces that are
  10878.              still used and that are not already dead or set.  */
  10879.  
  10880.               for (i = regno; i < endregno; i++)
  10881.             {
  10882.               rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
  10883.  
  10884.               if ((reg_referenced_p (piece, PATTERN (place))
  10885.                    || (GET_CODE (place) == CALL_INSN
  10886.                    && find_reg_fusage (place, USE, piece)))
  10887.                   && ! dead_or_set_p (place, piece)
  10888.                   && ! reg_bitfield_target_p (piece,
  10889.                               PATTERN (place)))
  10890.                 REG_NOTES (place) = gen_rtx (EXPR_LIST, REG_DEAD,
  10891.                              piece,
  10892.                              REG_NOTES (place));
  10893.             }
  10894.  
  10895.               place = 0;
  10896.             }
  10897.         }
  10898.         }
  10899.       break;
  10900.  
  10901.     default:
  10902.       /* Any other notes should not be present at this point in the
  10903.          compilation.  */
  10904.       abort ();
  10905.     }
  10906.  
  10907.       if (place)
  10908.     {
  10909.       XEXP (note, 1) = REG_NOTES (place);
  10910.       REG_NOTES (place) = note;
  10911.     }
  10912.       else if ((REG_NOTE_KIND (note) == REG_DEAD
  10913.         || REG_NOTE_KIND (note) == REG_UNUSED)
  10914.            && GET_CODE (XEXP (note, 0)) == REG)
  10915.     reg_n_deaths[REGNO (XEXP (note, 0))]--;
  10916.  
  10917.       if (place2)
  10918.     {
  10919.       if ((REG_NOTE_KIND (note) == REG_DEAD
  10920.            || REG_NOTE_KIND (note) == REG_UNUSED)
  10921.           && GET_CODE (XEXP (note, 0)) == REG)
  10922.         reg_n_deaths[REGNO (XEXP (note, 0))]++;
  10923.  
  10924.       REG_NOTES (place2) = gen_rtx (GET_CODE (note), REG_NOTE_KIND (note),
  10925.                     XEXP (note, 0), REG_NOTES (place2));
  10926.     }
  10927.     }
  10928. }
  10929.  
  10930. /* Similarly to above, distribute the LOG_LINKS that used to be present on
  10931.    I3, I2, and I1 to new locations.  This is also called in one case to
  10932.    add a link pointing at I3 when I3's destination is changed.  */
  10933.  
  10934. static void
  10935. distribute_links (links)
  10936.      rtx links;
  10937. {
  10938.   rtx link, next_link;
  10939.  
  10940.   for (link = links; link; link = next_link)
  10941.     {
  10942.       rtx place = 0;
  10943.       rtx insn;
  10944.       rtx set, reg;
  10945.  
  10946.       next_link = XEXP (link, 1);
  10947.  
  10948.       /* If the insn that this link points to is a NOTE or isn't a single
  10949.      set, ignore it.  In the latter case, it isn't clear what we
  10950.      can do other than ignore the link, since we can't tell which 
  10951.      register it was for.  Such links wouldn't be used by combine
  10952.      anyway.
  10953.  
  10954.      It is not possible for the destination of the target of the link to
  10955.      have been changed by combine.  The only potential of this is if we
  10956.      replace I3, I2, and I1 by I3 and I2.  But in that case the
  10957.      destination of I2 also remains unchanged.  */
  10958.  
  10959.       if (GET_CODE (XEXP (link, 0)) == NOTE
  10960.       || (set = single_set (XEXP (link, 0))) == 0)
  10961.     continue;
  10962.  
  10963.       reg = SET_DEST (set);
  10964.       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
  10965.          || GET_CODE (reg) == SIGN_EXTRACT
  10966.          || GET_CODE (reg) == STRICT_LOW_PART)
  10967.     reg = XEXP (reg, 0);
  10968.  
  10969.       /* A LOG_LINK is defined as being placed on the first insn that uses
  10970.      a register and points to the insn that sets the register.  Start
  10971.      searching at the next insn after the target of the link and stop
  10972.      when we reach a set of the register or the end of the basic block.
  10973.  
  10974.      Note that this correctly handles the link that used to point from
  10975.      I3 to I2.  Also note that not much searching is typically done here
  10976.      since most links don't point very far away.  */
  10977.  
  10978.       for (insn = NEXT_INSN (XEXP (link, 0));
  10979.        (insn && (this_basic_block == n_basic_blocks - 1
  10980.              || basic_block_head[this_basic_block + 1] != insn));
  10981.        insn = NEXT_INSN (insn))
  10982.     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  10983.         && reg_overlap_mentioned_p (reg, PATTERN (insn)))
  10984.       {
  10985.         if (reg_referenced_p (reg, PATTERN (insn)))
  10986.           place = insn;
  10987.         break;
  10988.       }
  10989.     else if (GET_CODE (insn) == CALL_INSN
  10990.           && find_reg_fusage (insn, USE, reg))
  10991.       {
  10992.         place = insn;
  10993.         break;
  10994.       }
  10995.  
  10996.       /* If we found a place to put the link, place it there unless there
  10997.      is already a link to the same insn as LINK at that point.  */
  10998.  
  10999.       if (place)
  11000.     {
  11001.       rtx link2;
  11002.  
  11003.       for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
  11004.         if (XEXP (link2, 0) == XEXP (link, 0))
  11005.           break;
  11006.  
  11007.       if (link2 == 0)
  11008.         {
  11009.           XEXP (link, 1) = LOG_LINKS (place);
  11010.           LOG_LINKS (place) = link;
  11011.  
  11012.           /* Set added_links_insn to the earliest insn we added a
  11013.          link to.  */
  11014.           if (added_links_insn == 0 
  11015.           || INSN_CUID (added_links_insn) > INSN_CUID (place))
  11016.         added_links_insn = place;
  11017.         }
  11018.     }
  11019.     }
  11020. }
  11021.  
  11022. void
  11023. dump_combine_stats (file)
  11024.      FILE *file;
  11025. {
  11026.   fprintf
  11027.     (file,
  11028.      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
  11029.      combine_attempts, combine_merges, combine_extras, combine_successes);
  11030. }
  11031.  
  11032. void
  11033. dump_combine_total_stats (file)
  11034.      FILE *file;
  11035. {
  11036.   fprintf
  11037.     (file,
  11038.      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
  11039.      total_attempts, total_merges, total_extras, total_successes);
  11040. }
  11041.